权限:
<!-- 开机启动 --> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.blt.phoneuse.BootCompleteReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
package com.blt.phoneuse; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import com.blt.framework.util.BLTLog; import com.blt.phoneuse.activity.process.ServiceProcess; public class BootCompleteReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { BLTLog.d("bltbltblt LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("bltbltblt LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("bltbltblt LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("bltbltblt LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("bltbltblt LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("bltbltblt LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("bltbltblt LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "bltbltblt recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "recevie boot completed ... "); BLTLog.d("LibraryTestActivity", "recevie boot completed ... "); Intent i = new Intent(context, ServiceProcess.class); i.setAction(ServiceProcess.PROCESS_GET); context.startService(i); } }
service中的内容,注意onStartCommand中的内容
package com.blt.phoneuse.activity.process; import android.app.IntentService; import android.app.Notification; import android.content.Intent; import android.util.Log; public class ServiceProcess extends IntentService { private final static String TAG = "ProcessService"; public final static String PROCESS_GET = "PROCESS_GET"; /** * 状态为锁屏 */ public final static String STATE_LOCK = "_SCREEN_LOCK"; public ServiceProcess() { super(TAG); } @Override protected void onHandleIntent(Intent intent) { Log.d(TAG, TAG); if (intent.getAction().equals(PROCESS_GET)) { // 自己要做的事 } } /** * 每次调用startService(Intent)的时候,<br> * 都会调用该Service对象的onStartCommand(Intent,int,int)方法,<br> * 然后在onStartCommand方法中做一些处理。 */ @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.v(TAG, "startCommand"); /* * START_STICKY:如果service进程被kill掉,保留service的状态为开始状态,但不保留递送的intent对象。 随后系统会尝试重新创建service,由于服务状态为开始状态,所以创建服务后一定会调用onStartCommand(Intent,int,int)方法。如果在此期间没有任何启动命令被传递到service,那么参数Intent将为null。 * START_NOT_STICKY:“非粘性的”。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统不会自动重启该服务。 START_REDELIVER_INTENT:重传Intent。使用这个返回值时,如果在执行完onStartCommand后,服务被异常kill掉,系统会自动重启该服务,并将Intent的值传入。 * START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保证服务被kill后一定能重启。 */ flags = START_STICKY;// 如果服务被异常kill掉,系统会自动重启该服务 Notification notification = new Notification(); notification.flags = Notification.FLAG_ONGOING_EVENT; notification.flags |= Notification.FLAG_NO_CLEAR; notification.flags |= Notification.FLAG_FOREGROUND_SERVICE; this.startForeground(1, notification); return super.onStartCommand(intent, flags, startId); } }
我用的这个方法开机启动 ,为了防止service被系统杀掉,通知栏会显示出我这个应用的通知 消除不掉
还有有的手机系统会有开机自启动的权限设置,比如我用的小米,需要进入 安全中心--》授权管理--》自启动管理,打开自己的应用
还有此方法是接受开机启动的广播,但是很多系统都屏蔽了这个广播,所以还有的思路就是用其他的广播启动,比如收到短信等等(话说 经过我的测试,如果不在手机上开启自动启动的权限 什么广播都没用,如果使用收到短信的广播还要加上收短信权限)
参考,使用mount的广播:http://www.itnose.net/detail/6178717.html
附一个广播大全~ http://blog.csdn.net/djun100/article/details/11020723
demo下载:http://s.cdz.la/file/7682096