Android 从广播接收器中杀死进程
您好,我希望在 USB 断开连接时终止应用程序中的一个活动,我有一个广播接收器,并且所有设置都设置正常并且工作正常。
public class USBOff extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, SdcardOff.class);
startActivity(intent1);
finish();
}
}
但是此代码告诉我需要创建 finish 方法和 startActivity 方法?为什么这不起作用谢谢您对我的问题的帮助
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 you for any help with my problem
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
startActivity
是一个 Context 方法,而不是一个 BroadcastReceiver 方法。finish
是一个 Activity 方法。试试这个:context.startActivity(intent1);
您无法从 BroadcastReceiver 调用
finish
。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.我找到了一种交互方法:
Manfest:
接收器代码:
活动onCreate部分:
如果您可能有许多活动实例,则在此类配置中它将无法工作(字符串android:launchMode =“singleInstance”对您来说是一个问题),
我希望它会有所帮助
I have found an interexting method :
Manfest :
receiver code :
activity onCreate part :
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
简单逻辑:
如果你不想终止该活动,那么就不要使用 finish() 如果你想终止该活动,那么你应该调用 finish();
所以去掉finish();从代码中尝试一下。
享受。
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.