如何在Android程序Force Closed后自动重启
2014-06-03 10:32
260 查看
1.在AndroidManifest中加入Service的注册代码和权限
<uses-permission android:name="android.permission.GET_TASKS" /><service
android:name="com.mo.ui.MyService"
android:exported="true" >
</service>
2.编写MyService代码
public class MyService extends Service
{
// 定义个一个Tag标签
private static final String TAG = "MyService";
// 这里定义吧一个Binder类,用在onBind()有方法里,这样Activity那边可以获取到
private MyBinder mBinder = new MyBinder();
private Timer timer;
@Override
public IBinder onBind(Intent intent)
{
Log.e(TAG, "start IBinder~~~");
return mBinder;
}
@Override
public void onCreate()
{
Log.e(TAG, "start onCreate~~~");
super.onCreate();
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId)
{
Log.e(TAG, "start onStart~~~");
super.onStart(intent, startId);
timer = new Timer();
timer.schedule(new TimerTask()
{
@Override
public void run()
{
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ComponentName componentName = activityManager.getRunningTasks(1).get(0).topActivity;
Log.d(TAG, "componentName.getClassName()====>" + componentName.getClassName());
· //在timer任务作判断
if (!componentName.getClassName().equals("com.mo.ui.MainUIActivity")
&&!componentName.getClassName().equals("com.mo.ui.ShowErrorActivity")
&&!componentName.getClassName().equals("com.mo.setting.SettingLoginActivity"))
{
Log.d(TAG, "restart Main UI Activity!");
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage("com.mo");//包名
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
else
{
}
}
}, 0, 30000);
}
@Override
public void onDestroy()
{
Log.e(TAG, "start onDestroy~~~");
super.onDestroy();
timer.cancel();
/*Intent localIntent = new Intent();
localIntent.setClass(this, MyService.class); //销毁时重新启动Service
this.startService(localIntent);*/
}
@Override
public boolean onUnbind(Intent intent)
{
Log.e(TAG, "start onUnbind~~~");
return super.onUnbind(intent);
}
// 这里我写了一个获取当前时间的函数,不过没有格式化就先这么着吧
/*public String getSystemTime() {
Time t = new Time();
t.setToNow();
return t.toString();
}
*/
public class MyBinder extends Binder
{
MyService getService()
{
return MyService.this;
}
}
}
3.在Activity的onCreate中开启
// 启动一个服务来监控程序的运行状态
Intent i = new Intent();
i.setClass(this, MyService.class);
this.stopService(i);//先将service停止,以防多次开启service
this.startService(i);//这种方法弹出的service起来后不会因为Activity的结束而结束
相关文章推荐
- 如何在Android系统启动后自动运行程序 [
- android 如何使得电阻屏在第一次开机时自动叫起屏幕校准程序
- Android程序的时候就要设计到编写xml布局文件,这里就教你如何设置xml的自动补全。
- 如何让A20,android开机自动启动C程序
- android如何添加桌面图标和卸载程序后自动删除图标
- C#中如何重启系统并在开机后自动执行当前可执行程序
- 【Android】App应用崩溃(Crash/Force Close)之后如何让它自动重启?
- 如何在 Android 程序中禁止屏幕旋转和重启Activity
- Android程序如何在开机时自动运行
- android如何使得电阻屏在第一次开机时自动叫起屏幕校准程序
- Android程序:系统重启+开机自动运行+屏幕解锁
- Android 如何实现download软件后自动重启 M
- 如何在 Android 程序中禁止屏幕旋转和重启Activity
- 如何在 Android 程序中禁止屏幕旋转和重启Activity
- Android 通过组合键进入recovery模式选择sdcard菜单升级,升级完如何自动重启?
- 如何在 Android 程序中禁止屏幕旋转和重启Activity
- android如何使得电阻屏在第一次开机时自动叫起屏幕校准程序
- 如何在 Android 程序中禁止屏幕旋转和重启Activity
- (转载整理)程序安装流程及如何实现文件在系统后自动重启(windows)
- 【Android项目】程序意外Crash后自动重启