您的位置:首页 > 移动开发 > Android开发

如何在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 service