Android开发之开机启动某应用以及定时重启应用

前言

最近因项目需求,在类似于ATM机上的android系统上开发一款app,需要实现开机即启动app,防止客户脱离app做其他操作,并且需要定时重启项目更新某些数据。

1.实现开机即启动思路

实际上,系统开机的时候就会发送一条开机的广播,我们要做的就是写一个开机启动的广播接收器,当接收到这条广播的时候,启动入口activity即可。

2.定时重启app思路

继承Application类,在onCreate()方法中注册了一个服务,此服务绑定于我们继承的Application类,然后在定时器(TimerTask)执行重启的逻辑。

主要代码

  • 开机启动主要代码
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * 开机广播接受者
 * @author jiangbing
 *
 */
public class BootBroadcastReceiver extends BroadcastReceiver {

    private static final String ACTION = "android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (ACTION.equals(intent.getAction())) {
            // 开机即启动此应用
            Intent mIntent = new Intent(context, MainActivity.class);
            mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mIntent);
        }
    }

}
  • 定时重启

App.java

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.widget.Toast;

public class App extends Application {

    private ServiceConnection conn = new ServiceConnection() {

        /**
         * Called when a connection to the Service has been established, 
         * with the android.os.IBinder of the communication channel to the Service.
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            RestartAppService.MyBinder mBinder = (RestartAppService.MyBinder) service;
            mBinder.startRestartTask(App.this);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {}

    };

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "app启动了", Toast.LENGTH_LONG).show();
        // 启动服务,用于定时重启app
        Intent intent = new Intent(this, RestartAppService.class);
        bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }
}

RestartAppService.java

import java.util.Timer;
import java.util.TimerTask;

import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;

/**
 * 重启app服务
 * @author jiangbing
 *
 */
public class RestartAppService extends Service {

    private static final long RESTART_DELAY = 30 * 1000; // 多少时间后重启
    private MyBinder mBinder;

    // 此对象用于绑定的service与调用者之间的通信
    public class MyBinder extends Binder {

        /**
         * 获取service实例
         * @return
         */
        public RestartAppService getService() {
            return RestartAppService.this;
        }

        /**
         * 启动app重启任务
         */
        public void startRestartTask(final Context context) {
            Toast.makeText(context, "start", Toast.LENGTH_SHORT).show();

            TimerTask task = new TimerTask() {

                @Override
                public void run() {
                    // restart
                    Intent intent = getPackageManager().getLaunchIntentForPackage(
                            getApplication().getPackageName());
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    System.exit(0);
                }
            };

            Timer timer = new Timer();
//          long delay = 0;
//          long intervalPeriod = 1 * 1000;
            timer.schedule(task, RESTART_DELAY);
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // Create MyBinder object
        if (mBinder == null) {
            mBinder = new MyBinder();
        }
        return mBinder;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}

点此下载demo

你可能感兴趣的:(Android开发之开机启动某应用以及定时重启应用)