开发者

Android kill process from broadcast receiver

开发者 https://www.devze.com 2023-03-13 13:18 出处:网络
Hi i\'m looking to kill an activity in my application when the usb is disconnected i have a broadcast receiver and all set up and working fine

Hi i'm looking to kill an activity in my application when the usb is disconnected i have a broadcast receiver and all set up and working fine

public class USBOff extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Intent intent1 = new Intent(context, SdcardOff.class);
    startActivity(intent1);
    finish();
    }
 }

This code however tells me that the finish method and startActivity methods need to be created? why is this not working thank yo开发者_JAVA百科u for any help with my problem


startActivity is a Context method, not a BroadcastReceiver method. finish is an Activity method. Try this:

context.startActivity(intent1);

You can't call finish from a BroadcastReceiver.


I have found an interexting method : Manfest :

android:launchMode="singleInstance"
<action android:name="com.gr.app.KILLSELF" />

receiver code :

Intent intentInterface = new Intent("com.gr.app.KILLSELF");
mContext.startActivity(intentInterface);

activity onCreate part :

if (intent.getAction().compareTo("com.gr.app.KILLSELF") == 0) {
        finish();
    };

It will not work in such configuration if you may have many instancesof activity (string android:launchMode="singleInstance" is a problem for you),

I hope it will help


Simple Logic :

If you dont want to kill the activity then do not use finish() And if you want to kill the activity then you should have to call finish();

So remove the finish(); from code and try it out. Enjoy.

0

精彩评论

暂无评论...
验证码 换一张
取 消