Android 如何通过广播接收器将应用程序从后台带到前台
我有两个类,分别是MainActivity和MyBroadcastReceiver。BroadcastReceiver检测手机屏幕是打开还是关闭。我的愿望是在屏幕锁释放时启动我的应用程序。我的意思是,当手机锁释放时,我想把我的应用程序放在最前面 这是我的活动课:Android 如何通过广播接收器将应用程序从后台带到前台,android,broadcastreceiver,intentfilter,powermanager,Android,Broadcastreceiver,Intentfilter,Powermanager,我有两个类,分别是MainActivity和MyBroadcastReceiver。BroadcastReceiver检测手机屏幕是打开还是关闭。我的愿望是在屏幕锁释放时启动我的应用程序。我的意思是,当手机锁释放时,我想把我的应用程序放在最前面 这是我的活动课: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerReceiver();
}
private void registerReceiver(){
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new MyPhoneReceiver();
registerReceiver(mReceiver, filter);
}
}
这是我的广播接收器:
public class MyPhoneReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if(pm.isScreenOn()){
//Bring application front
}
}
}
要在广播接收器中执行此操作,我应该做什么?尝试使用或尝试使用或在onReceive方法中执行此操作:
Intent activityIntent = new Intent(this, MainActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
您可能需要根据您的具体需要调整addFlags调用 在onReceive方法中执行此操作:
Intent activityIntent = new Intent(this, MainActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
您可能需要根据您的具体需要调整addFlags调用 在BroadcastReceive的onReceive方法中执行以下操作
@Override
public void onReceive(Context context, Intent intent) {
Intent newIntent = new Intent();
newIntent.setClassName("com.your.package", "com.your.package.MainActivity");
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(newIntent);
}
您需要使用“flag_ACTIVITY_NEW_TASK”标志来表示您的意图,否则将引发致命异常“flag_ACTIVITY_SINGLE_TOP”标志将把MainActivity带到最前面,通过覆盖MainActivity中的onNewIntent方法,您可以从那里继续执行任何操作
@Override
protected void onNewIntent(Intent intent) {
// continue with your work here
}
在BroadcastReceiver的onReceive方法中执行以下操作
@Override
public void onReceive(Context context, Intent intent) {
Intent newIntent = new Intent();
newIntent.setClassName("com.your.package", "com.your.package.MainActivity");
newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(newIntent);
}
您需要使用“flag_ACTIVITY_NEW_TASK”标志来表示您的意图,否则将引发致命异常“flag_ACTIVITY_SINGLE_TOP”标志将把MainActivity带到最前面,通过覆盖MainActivity中的onNewIntent方法,您可以从那里继续执行任何操作
@Override
protected void onNewIntent(Intent intent) {
// continue with your work here
}
嗨,我是新安卓和xamarin,我的应用程序中有相同的问题,我想在接听电话时打开应用程序。我不知道我需要在“com.your.package”、“com.your.package.main活动”中给出什么,你能帮我吗?我需要在manifest中设置任何内容?查看manifest.xml文件并查找Hi我是新安卓和xamarin,我的应用程序中有相同的问题,我想在接听电话时打开应用程序。我不知道我需要在“com.your.package”、“com.your.package.main活动”中给出什么,你能帮我吗?我需要在manifest中设置任何内容?查看manifest.xml文件并查找