Resume
有BroadcastReceiver
个应用的方法吗? (以启动器恢复或启动应用程序的方式)
我希望在插入设备时执行此操作(不用担心,这不会出现在Play商店中)
您似乎必须使用FLAG_ACTIVITY_NEW_TASK
,否则您会收到RuntimeException
"Calling startActivity() from outside of an Activity context"
public class PowerReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.ACTION_POWER_CONNECTED")) {
final Intent newIntent = new Intent(context, FooActivity.class);
//newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(newIntent);
}
}
}