How make Android not restart app when application stops - android

I'm trying to make app close when some uncaughtException happens, but android show a message to try to restart the application and the application stays in slow motion or just black screen, I made a class to extends in my activity to overriding the method uncaughtException() to catch logs to send than to sentry.io and using:
public class MyExceptionHandler implements
Thread.UncaughtExceptionHandler {
public MyExceptionHandler(Activity context) {
Thread.setDefaultUncaughtExceptionHandler(this);
}
#Override
public void uncaughtException(final Thread thread, final Throwable ex) {
Log.e("ERROR", ex.toString());
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}
}

You can use
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Got idea from this answer.

Related

Intent from executors for reciever is not sent

I can’t catch up - how to send Toast from Executors.newSingleThreadExecutor () in the run () method? In the debug I set a breakpoint, everything is fine, we go into the method, but the message does not appear in the emulator? And yet, in the same method, I send the intent to the receiver, the intent is sent, but the receiver does not receive it.
ExecutorService service:
public void start() {
service = Executors.newSingleThreadExecutor();
service.submit(new Runnable() {
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent(ACTION_FOR_FRAGMENT);
sendBroadcast(intent);
service.shutdown();
}
});
}
I solved this problem, you can close the question. The essence of the problem was that I did not correctly implement the service methods.

Intent Service blocking the UI

I need to make so backround job without blocking user UI, so I made a test IntentService to check how it's work, but this intent services block the UI.
Here my Code on start app ic start service:
Intent msgIntent = new Intent(this, AutoExchange.class);
startService(msgIntent);
AutoExchange.java
public class AutoExchange extends IntentService{
public AutoExchange() {
super("AutoExchange");
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
The problem is that this service block the main Ui, somethig I did wrong?
instead of thread.sleep() you can you handler
here is an example
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
// do stuff
}
},3000);
Try to make a new Runnable() around the try-catch-block.
Caution: A service runs in the main thread of its hosting process; the service does not create its own thread and does not run in a separate process unless you specify otherwise.
Source
Register your service in android manifest.

IntentService - Wakelock release issue

I have an alarm application.
Flow looks like this :
WakefulBroadcastReceiver(Acquires wakelock) --->> Intent service -->> startActivity
public class AlarmService extends IntentService {
protected void onHandleIntent(Intent intent) {
Intent activityIntent = new Intent(this, TriggeredActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(activityIntent);
Basically WakefulBroadcaseReceiver starts an intent service using startWakefulService(). Inside intent service's onHandleIntent(), only work I am doing is further starting a new activity using startActivity(). That new activity is where I am using mediaPlayer in a loop, which sounds the alarm. That activity has a dismiss button, which waits for user click to stop the media player & activity finishes.
Now the problem I am facing is that after calling startactivity() inside intent service, I can not wait for TriggeredActivity to finish(no equivalent to startActivityForResult in Service) and then complete wakeful intent. Related link
startActivity(activityIntent);
WakefulBCastReceiver.completeWakefulIntent(intent); /* can't do here */
So I am not explicitly releasing wakelock here.
My question is will the wakelock be released automatically(link-to-death), when the process that is holding it is killed.
If yes, then in my particular scenario, I need not call WakefulBCastReceiver.completeWakefulIntent().
Yes, you need to use completeWakefulIntent.
You need to put your TriggeredActivity intent into EXTRAs.
#Override
public void onReceive(Context context, Intent intent) {
Intent intentService = new Intent(context, NotificationsIntentService.class);
// Inserting data inside the Intent
intentService.putExtra(NotificationsIntentService.EXTRA_NOTIF, new Intent(context, TriggeredActivity.class));
startWakefulService(context, intentService);
}
NotificationsIntentService.class
public class NotificationsIntentService extends IntentService {
private static final String TAG = "DebugNotifIntent";
public static final String EXTRA_NOTIF = "extra_notif";
public NotificationsIntentService(){
super(NotificationsIntentService.class.getSimpleName());
}
#Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent: ");
Intent extraIntent = intent.getParcelableExtra(EXTRA_NOTIF);
extraIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(extraIntent);
NotificationWakefulBroadcastReceiver.completeWakefulIntent(intent);
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}
I have managed to find a solution for my problem. I am now using a Messenger for message based cross process communication between intent service & triggered activity.
I am passing a handler - alarmServiceHandler, from intent service to activity through a messenger.
Handler alarmServiceHandler = new Handler(){
#Override
public void handleMessage(Message msg) {
if(msg.arg1 == 1) {
completedTriggeredActivity = true;
}
}
};
Inside onHandleIntent(), I am passing handler through Messenger object in intent's extra data.
Messenger alarmServiceMessenger = new Messenger(alarmServiceHandler);
Intent activityIntent = new Intent(this, TriggeredActivity.class);
activityIntent.putExtra("AlarmServiceMessenger", alarmServiceMessenger);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(activityIntent);
while(!completedTriggeredActivity){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
WakefulBCastReceiver.completeWakefulIntent(intent);
In TriggeredActivity, I am retrieving messenger in Dismiss button's OnClickListener, just before calling finish() on the activity. And sending back a message to AlarmService with arg = 1, implying end of processing in triggered activity.
buttonDismiss.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Messenger alarmServiceMessenger = getIntent().getParcelableExtra("AlarmServiceMessenger");
Message alarmServiceMessage = Message.obtain();
alarmServiceMessage.arg1 = 1;
try {
alarmServiceMessenger.send(alarmServiceMessage);
} catch (RemoteException e) {
e.printStackTrace();
}
finish();
}
After starting triggered activity, I am putting AlarmService in sleep mode till boolean variable completedTriggeredActivity has not been set to true in handleMessage(). Once true, it means triggered activity has finished & now I can proceed with releasing wake lock.
I would be glad to receive comments about my approach & any suggestions towards a better solution to my problem, than the one I have deviced.

intent service does not restart after my app crashes

So I'm calling my intent service on alarm manager but that intent service does not work after my app get crashed.
Is it possible for intent service to not work after your app get crashed?
Thank You.
Is it possible for intent service to not work after your app get
crashed?
You shall catch the crash event with an uncaught exception handler and then cancel the alarm something like below,
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread t, Throwable e) {
if (myAlarmMgr!= null) {
myAlarmMgr.cancel(myIntentServiceInstance);
}
}
});

Finishing an activity from service

I am building an app which requires a connection to an MQTT server. Everything works fine, but I want to make it fail proof. More specifically, I need that when the service is unable to connect to the MQTT server, the application closes as it can't do anything. I tried to broadcast an intent to the activity, but the broadcast doesn't reach it after catching the exception. (I ommited some code for easier understanding)
Service's onHandleIntent()
try{
mqttClient.connect();
} catch (MqttException e) {
Intent msgint = new Intent();
msgint.setAction(constantStrings.SH_CONN_ERROR);
sendBroadcast(msgint);
Log.d("MqttLog","ERROR-MqttException " + e.getMessage());
}
The Activity I need to finish (SelectionGrid.class)
#Override
protected void onCreate(Bundle savedInstanceState) {
[...]
IntentFilter filter = new IntentFilter();
filter.addAction(constantStrings.SH_CONNERROR);
registerReceiver(SH_Communication, filter);
}
BroadcastReceiver SH_Communication = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(constantStrings.SH_CONN_ERROR)){
new AlertDialog.Builder(getApplicationContext())
.setTitle("Error")
.setMessage("No conection")
.setCancelable(false)
.setPositiveButton("Close app", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
SelectionGrid.this.finish();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}
};
I do get the Log entry when catching the exception, but the broadcast never gets to the activity.
Is there any better way to do this?
I was able to solve it. It turned out that I was calling the wrong activity which looks a lot like the one I really needed. The code that I showed in the question is correct.

Categories

Resources