Android开发之开机启动某应用以及定时重启应用
2015-12-30 10:23
411 查看
前言
最近因项目需求,在类似于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; < 4000 span class="hljs-keyword">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开发规范
- Android Studio教程从入门到精通
- Android简单计算器
- Android内存优化(使用SparseArray和ArrayMap代替HashMap)
- android permission权限与安全机制解析(下)
- Android编程使用缓存优化ListView的方法
- Android handler 内存泄露分析及解决方法
- 2015年十大热门Android开源新项目
- android利用annotation代替findViewById()
- Android学习笔记之AndroidManifest.xml文件解析
- android-Permissions Best Practices
- Android开发异常篇:运行工程出现:Connection with adb was interrupted错误提示以及解决办法
- Android中嵌入极光推送SDK存在冲突问题
- 【学习】Android代码混淆与反编译
- Android和JavaScript相互调用的方法
- AndroidStudio创建一个新的工程
- Android Fragment完全解析,关于碎片你所需知道的一切 (转)。
- Bitmap基本概念及在Android4.4系统上使用BitmapFactory的注意事项
- Android 系统Action大全
- android 5大布局