My current Android Application needs to call
ActivityManager.clearApplicationUserData()
to simulate the user clearing App storage
Which works fine.
A side effect of calling clearApplicationUserData() is that the App is (understandably) closed.
Which gives a poor user experience.
I am having difficulty in restarting my Application once I have called clearApplicationUserData().
I have tried using startActivity, Alarm Manager with Pending Intent, Foreground/Background service.
Nothing works.
Is it impossible to restart an Android App having called clearApplicationUserData()?
(1st answer: this answer only works on limited situations. it's not a complete answer)
public boolean clearApplicationUserData ()
Description
Return: true if the application successfully requested that the application's data be erased; false otherwise.
As the reference website stated, we have a returnee before the application is being closed. so, we are going to use this returnee in order to restart the app.
if(ActivityManager.clearApplicationUserData)
{
doRestart = true;
}
when Activity onDestroy() and onStop() are called restart app.
#Override
protected void onDestroy() {
super.onDestroy();
if(doRestart){
Intent intent = new Intent(this, Activity.class);
this.startActivity(intent);
}
}
#Override
protected void onStop() {
super.onStop();
if(doRestart){
Intent intent = new Intent(this, Activity.class);
this.startActivity(intent);
}
}
We put restart action in both onDestroy() and onStop() in order to make sure the app will be restarted again.
And also, I think it's a good idea to force stop activity before OS stops it.
if(ActivityManager.clearApplicationUserData)
{
doRestart = true;
finish(); <= i mean this
}
it's because it makes sure that onDestroy() and onStop() will be invoked.
My suggestion might sound trivial but, have you consider not calling ActivityManager.clearApplicationUserData()?
Here what the docs says about this method:
Permits an application to erase its own data from disk. This is
equivalent to the user choosing to clear the app's data from within
the device settings UI. It erases all dynamic data associated with the
app -- its private data and data in its private area on external
storage -- but does not remove the installed application itself, nor
any OBB files.
So in order to mimic this behavior you just need to clear you internal and external storage directories. No permissions are needed to access any of those.
(2nd answer: I need much more contribution on it)
After 8 hours of researching in Android OS and Android Developers Website in order to find a solution to restart activity when clearApplicationUserData is invoked. Finally, I would be able to find a nice/hacking solution.
This solution looks like Zidane dribble :)
Let's introduce the solution. at first, clearApplicationUserData clears all the clues of the application when is invoked like tasks, notifications, alarms and etc. therefore, explicit Activity calling is impossible.
implicit way is the only possible way of calling activity.
After a couple of tests I found that application manifest registered intent-filters wouldn't be removed and they are able to listen for incoming system broadcasts.
Approximately, 98% of system broadcasts wouldn't be received by cleared application and that 2% remained might not be broadcasted very soon.
so what to do? hmmm? come on man I must find a solution ...
bingo, ** I must trigger something in order to system broadcast it** <= looks hacking :)
so I decide to choose WIFI_STATE_CHANGED because
Easy Access permission
System Broadcasts it on delay <= this makes sure that the app is closed
before broadcast
manifest.xml
<receiver
android:name=".PackageDataClearedReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
MainActivity.java
public class MainActivity extends AppCompatActivity {
ActivityManager am;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatButton btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new Thread(new Runnable() {
#Override
public void run() {
am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
if (am != null) {
ExecutorService pool = Executors.newFixedThreadPool(2);
final Collection<Future> futures = new HashSet<Future>();
futures.add(pool.submit(new Runnable() {
#Override
public void run() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
wifiManager.setWifiEnabled(false);
am.clearApplicationUserData();
}
}));
for (Future future : futures) {
future.isDone();
}
}
}
}).start();
}
});
}
}
demo
keep in mind, it's just A minimum viable product that needs to be developed more in order to make it work perfectly.
Clearing the app data on the device through the API clearApplicationUserData() resets the app as if it were just installed. As you have found, any alarms and broadcasts registered with your app are also cleared. The most efficient way to keep your app in the foreground would be to clear the data yourself, as others have pointed out, rather than using the API. Here is an example: https://stackoverflow.com/a/9073473/949224
However, if you are determined to use the API (which does guarantee all data is cleared) and the app is force-stopped, I have a suggestion:
Create a small companion app that can be launched just prior to you clearing your app data. The companion app simply re-launches your app, possibly after a short time-out.
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("example.com.testrelaunchapp");
if (launchIntent != null) {
startActivity(launchIntent);//null pointer check in case package name was not found
} else {
Log.w( TAG, "Unable to resolve launch activity of relauncher companion app");
}
((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE))
.clearApplicationUserData();
The companion app itself needs to close afterwards, and ideally should be hidden from the Activity Stack etc..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent launchIntent = getPackageManager().getLaunchIntentForPackage("example.com.yourmainapp");
if (launchIntent != null) {
Handler handler = new Handler(getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
Log.i( TAG, "About to act on launchIntent");
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(launchIntent);
finish();
System.exit(0);
}
}, 1000);
}
}
I have seen this work with Android 6.0, but no guarantees it would be versatile and work across the board. There would be more to do to make the companion app UI-less if desired and to be hidden from the phone's launcher. You would also probably want to bundle the APK as a file within your own app and install it upon first run, which would need the user to enable installation from "Unknown sources" (non-Play store). It can be done through intents to the right System settings, if needed, but users would need a good explanation why this is needed.
So, as I was saying, the simpler approach is to clear the data and app permissions yourself.
Related
I'm currently developing an app that requires the Android's location service to always be active.
Right now I'm able to detect if the user activates or deactivates the location service when he resumes the app (in the onResume() event ).
The problem is that, if the user deactivates the location service throught the quick settings menu, I am not able to detect the event.
How can I know when the user activates / deactivates the device's location through the quick settings?
Thank you.
My code:
...
BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
// Make an action or refresh an already managed state.
Log.d("vtApp", "CHANGED");
}
}
};
currentActivity.registerReceiver(mGpsSwitchStateReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));
References I followed:
How to trigger broadcast receiver when gps is turn on/off?
Android Quick Settings notifications?
Changing Location Settings
I had the same problem.
this worked for me
intentFilter.addAction(android.location.LocationManager.PROVIDERS_CHANGED_ACTION);
not to confuse it with
Intent.ACTION_PROVIDER_CHANGED
How to continue background service in android Oreo without showing notification dot? i continues my background service using notification but i don't want to show notification for running service.
If you would have read the Android Oreo 8.0 Documentation properly somewhere in here, you might not have posted this question here.
Step 1: Make sure you start a service as a foreground Service as given in below code
ContextCompat.startForegroundService(mainActivity, new Intent(getContext(), GpsServices.class));
ContextCompat.startForegroundService(mainActivity, new Intent(getContext(), BluetoothService.class));
ContextCompat.startForegroundService(mainActivity, new Intent(getContext(), BackgroundApiService.class));
Step 2: Use notification to show that your service is running. Add below line of code in onCreate method of Service.
#Override
public void onCreate() {
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(NOTIFICATION_ID, notification);
}
...
}
Step 3: Remove the notification when the service is stopped or destroyed.
#Override
public void onDestroy() {
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true); //true will remove notification
}
...
}
One problem with this solution is that it will keep showing the notification until your Service is running on all devices running on Android Oreo 8.0.
I'm sure that this solution will work even when the app is in the background or in kill state.
IMPORTANT NOTE: SHOWING A NOTIFICATION FOR RUNNING A SERVICE IN BACKGROUND (APP IN BACKGROUND OR KILLED STATE) IS MANDATORY IN ANDROID OREO 8.0. YOU CANNOT RUN AWAY WITH IT. SO IT IS RECOMMENDED THAT YOU MAKE NECESSARY CHANGES IN YOUR APP TO MAKE IT WORK PROPERLY AS PER THE BEST PRACTICES FOLLOWED OR ASKED BY ANDROID.
I hope this might help to solve your problem.
This isn't possible in this API version (26) and higher. Android OS automatically close your service if you run it without showing a notification to the user.
If you are targeting API >= 26 the system will impose a restriction on your service to run in background unless your activity in foreground. As soon as your activity goes to background, the service will be terminates when system finds it running in background (See Background service limitations).
By using startForegroundService() method you grant that permission to run the service in background even when the activity isn't running.
Must also once the service has been created, the service must call its startForeground() method within five seconds.
This code worked for me. First you make sure :
1- Define the service in Manifest
2- Define the permission in Manifest
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
3- Make sure you have started the service before.
Your service should have a notification. You should be careful that
your notification must have a ChannelID !
this page can help you:
How to create a Custom Notification Layout in android?
Android how to show notification on screen
Now paste the following code into your service
#Override
public void onCreate() {
super.onCreate();
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(" your Notification ID ", notification);
}
} catch (Exception e) {
Log.e(" Error --->> ", e.getMessage());
}
}
after that add this code:
#Override
public ComponentName startForegroundService(Intent service) {
return super.startForegroundService(service);
}
and this:
#Override
public void onDestroy() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true);
}
}
I have a service which plays music when the page is loaded, and carries on to all the other pages as it should. It still played when the application was destroyed, so i put a onDestroy method and an onResume method.
#Override
public void onDestroy(){
super.onDestroy();
Intent ServiceMusic = new Intent(this, BackgroundMusic.class);
stopService(ServiceMusic);
}
#Override
public void onResume(){
super.onResume();
Intent ServiceMusic = new Intent(this, BackgroundMusic.class);
startService(ServiceMusic);
}
Now the problem is when the user clicks the home button the service carries on, so I implemented a onPause and it worked fine, just when the user goes to another page the music restarts.
#Override
public void onPause(){
super.onPause();
Intent ServiceMusic = new Intent(this, BackgroundMusic.class);
stopService(ServiceMusic);
}
How can i overcome this so the music carries on playing when going through the application but when the Home Button is clicked i pause the service?
I would remove the code that is in individual activities and use the registerActivityLifecycleCallbacks mechanism in your application subclass. This will allow you to keep the code in one place and be smarter about starting and stopping your service.
Be aware that with these callbacks, you'll be notified that an Activity has stopped before another activity has started, so you will need some kind of delay when you decide to stop your service. You can use a handler with a delayed runnable to account for this issue.
https://developer.android.com/reference/android/app/Application.html#registerActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks)
I am having difficulties trying to handle service behaviour in such case.
Basically I have a service running on a separate process that needs to issue httprequests every certain time whenever the app is closed, then write something into preferences or throw a notification.
The logic works fine. The problem I'm having is that I cannot find a way to properly stop/disable that service whenever the app is running, aswell as start it again when the app is being finished or put into background.
I've tried stopping it at #onResume()/#onStart() callbacks of my activities aswell as starting it at #onStop()/#onDestroy() but behaviour doesnt run as expected in any case...
I'll paste here some code snippets of what i've tried so far:
I start/stop services using:
stopService(new Intent(this,NotificationService.class));
startService(new Intent(this, NotificationService.class));
Random activity from my app (all implement this in their callbacks):
#Override
protected void onResume() {
if (Utility.isMyServiceRunning(this)){
Utility.serviceClose(this);
}
super.onResume();
}
#Override
protected void onStop() {
if (!Utility.isMyServiceRunning(this)){
startService(new Intent(this, NotificationService.class));
}
super.onStop();
}
This somehow doesnt work or brings unexpected behaviour since the app moves from many activities, and service ends up being alive when the app is running or stopped when the app is in background/finished.
I've also tried to toggle on/off service logic on service timertask every cicle by asking:
#Override
public void run() {
ActivityManager activityManager = (ActivityManager) getBaseContext().getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++){
if(procInfos.get(i).processName.equals("com.example.myapp")) {
return;
}
}
//service http request logic here
}
But that doesnt work either because process "com.example.myapp" never gets killed (and of course I cannot/want to force finish that), so it never issues any httprequest.
Any Ideas on how to implement this? Any help would be very welcome.
Thanks in advance.
How about binding to your service and then communicating directly with it? Implement a simple on/off boolean, expose a getter/setter on the binding, and then make sure the service checks the boolean before it does any work. That way you can disable it while the app is running without having to actually start/stop the service repeatedly.
http://developer.android.com/guide/components/bound-services.html
Solved with better handling of onStop() onResume() callbacks.
The title explains all... I have this snippet of code in my application:
String url = createTelUrl("3112007315");
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(url));
context.startActivity(intent);
It does make a call, but once the call ends, my application crashes. I'd like to return to my application once the call has finished, but I read this post and it seems not to be possible. So... is there anyway to at least pause my application and resume it once the call has finished?
EDIT:
Thanks for the two answers I received, I feel I'm really close to my goal... I had already done some of the things you guys suggested. But, maybe I didn't explain some details of the application... I'm developing Who Wants To Be A Millonarie game, so I need to implement calls (I don't know how it's called in USA or other countrys, but here we call it "call to a friend").
Anyway... I've done too many changes to this app and now it's not crashing. But, the Canvas where I draw the UI is not been showed once the called has ended.
I have a SurfaceView that holds the UI. For that SurfaceView I created a thread that is meant to refresh the UI... this is basically what the thread does:
#Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
// Check if should wait
synchronized (_surfaceHolder) {
_panel.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
But, once the call has ended I get a black screen. The surface is there (I know it because it still can receive some touch events), but it's not showing anything. Another thing to take in account is how I'm starting the Thread from the SurfaceView class:
public void surfaceCreated(SurfaceHolder holder) {
hilo.setRunning(true);
try{
hilo.start();
}catch(IllegalThreadStateException ite){
Log.e("wwtbam", "god dammed");
}
}
This worked nice before I start implementing phone-calls. The problem here is that once the call has ended and it executes again the start method which throws a IllegalThreadStateException because the thread has already been started. I've tried using some 'technics' to pause the UI thread while calling but I haven't been able to solve this problem. I tried doing something like:
// this in the UI thread class
if(haveToWait)
wait();
....
// this in the surface view class
if(callEnded)
hilo.notify();
But that didn't work. I also have tried some other 'tricks' like using sleep(50); instead of wait(); but it does not work either.
With all that information I provided... what could you suggest to me?
The problem here is the place you're using to start the thread. Once you start a new call, your main activity will be paused and the surfaceview will be destroyed. Though, the thread will keep running. So, once your app takes the control back, the surface is created again and the start method will be invoked. That causes a IllegalThreadStateException.
The way to go here is to manipulate the thread out of the SurfaceView class. That will give you the control of the thread from the main activity, and you will be able to decide when to start or pause your thread.
Take a look of this example: http://code.google.com/p/apps-for-android/source/browse/trunk/SpriteMethodTest/src/com/android/spritemethodtest/
This is possible using an android.telephony.PhoneStateListener.
First, we need to take care of the manifest of the app:
We need the permission to make calls (duh!) as well as the permission to watch the phone state. The latter is needed so the app can react to the ending of a call as well. So we add these lines to out application manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
Also, we don't want Android to start a different instance of our activity when the call has ended, so we set the launchMode attribute of the activity to "singleInstance".
<activity android:name=".CallTest" android:label="Calling Test"
android:launchMode="singleInstance" />
Having prepared everything in the manifest, we can now look at the activity making the call:
public class CallTest extends Activity {
PhoneStateListener mListener;
TelephonyManager mTelMgr;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListener = new CallEndedListener();
mTelMgr = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
}
public void makecall(View v) {
// Register our listener to be notified of the beginning
// and ending of calls
mTelMgr.listen(mListener, PhoneStateListener.LISTEN_CALL_STATE);
// Start the call
Intent call = new Intent(Intent.ACTION_CALL);
call.setData(Uri.parse("tel:12345"));
startActivity(call);
}
class CallEndedListener extends PhoneStateListener {
boolean called = false;
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
// Don't fire before the call was made
if (state == TelephonyManager.CALL_STATE_OFFHOOK)
called = true;
// Call has ended -- now bring the activity back to front
if (called && state == TelephonyManager.CALL_STATE_IDLE) {
called = false;
mTelMgr.listen(this, PhoneStateListener.LISTEN_NONE);
startActivity(new Intent(CallTest.this, CallTest.class));
}
}
}
}
The only new thing in the makecall method, compared to the code snippet in the question, is the PhoneStateListener implementation added right before actually making the call. This listener then gets notified by Android when an outgoing call is dialed, an incoming call is ringing or when an active call is ended.
Our implementation waits for the latter CALL_STATE_IDLE event and starts our activity again, so that after the call has ended we're back in our app where we left it. It then deregisters itself, so our activity doesn't get restarted every time the user ends a call not initiated by our own activity.
However, when registering for the CALL_STATE-events with the TelephonyManager, Android instantly fires a notification with the current status -- so our listener would get triggered before the call had even started. Therefore our listener implementation first waits until an outgoing call was started (CALL_STATE_OFFHOOK) and only after that happened reacts to the CALL_STATE_IDLE notification.
HTH!
As for the crash - please post the log and put your debugger onStart/onResume to find out why you're crashing. It's possible that something is initialized in the wrong place and you might something as simple as nullpointer.
As for the call end thing - i've never try this, but i'd try to register receiver, catch
http://developer.android.com/reference/android/telephony/TelephonyManager.html#ACTION_PHONE_STATE_CHANGED
Evaluate the state of the phone and do what you need to do .
Also there is more info here
http://developer.android.com/reference/android/telephony/
http://developer.android.com/reference/android/telephony/PhoneStateListener.html
And finally you'll find examples of how to use that in the applications that are use that functionality in source.android.com