Android关闭进程后重启方法
2016-10-20 18:45
519 查看
由于应用需要关闭进程后重启,并非在原内存中重启。网上的方法全部是在内存中重启。只能在原进程中重启。
首先:新建一个广播接收器。
AndroidManifest.xml文件中注册一个<receiver>
<receiver android:name=".receiver.RestartAppReceiver">
<intent-filter>
<action android:name="restart.app"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
然后在需要重启的地方,关闭进程前先延时发送一个广播在广播中重启。
RestartAppReceiver.java
public class RestartAppReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("restart.app")){
Log.e("TAG","接受到广播");
Log.e("TAG", Process.myPid()+"");
Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
}
}
在需要重启的地方发送广播:
首先:新建一个广播接收器。
AndroidManifest.xml文件中注册一个<receiver>
<receiver android:name=".receiver.RestartAppReceiver">
<intent-filter>
<action android:name="restart.app"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
然后在需要重启的地方,关闭进程前先延时发送一个广播在广播中重启。
RestartAppReceiver.java
public class RestartAppReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("restart.app")){
Log.e("TAG","接受到广播");
Log.e("TAG", Process.myPid()+"");
Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
}
}
在需要重启的地方发送广播:
//关闭App并且重启 Log.e("DeviceInfoAty",Process.myPid()+""); sendBroadcast(new Intent("restart.app")); new Handler().postDelayed(new Runnable() { @Override public void run() { Process.killProcess(Process.myPid()); } },3000);
相关文章推荐
- Android平台中进程与线程以及关闭进程的一些方法
- 【Android】【转】Android程序退出彻底关闭进程的方法
- Android程序关闭进程的方法
- Android程序退出彻底关闭进程的方法
- Android程序退出彻底关闭进程的方法
- android 进程自杀再重启的方法
- Android 在进程中重启app的方法
- Android程序退出彻底关闭进程的方法
- Android程序退出彻底关闭进程的方法
- 【Android】【转】Android程序退出彻底关闭进程的方法
- 不重启Linux服务器关闭SeLinux的方法
- windows mobile 5.0 进程管理、窗体管理、重启和关闭操作系统(转载)
- C#关闭excel进程的最有效方法
- c#程序强制关闭所有进程方法(转载)
- windows mobile 5.0 进程管理、窗体管理、重启和关闭操作系统
- C#和Asp.net下调用Excel后无法自动关闭进程的解决方法
- cidaemon.exe进程cpu占用率高及关闭cidaemon.exe进程方法
- 远程关闭与重启Tomcat的方法
- Linux操作系统中,重启/关闭Weblogic服务器的方法
- windows mobile 5.0 进程管理、窗体管理、重启和关闭操作系统