如何在Android程序Force Closed后自动重启

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的结束而结束



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值