Android--上怎样让一个Service开机自动启动

注:网上基本上清一色的只有第二种写法.

1.首先开机启动后系统会发出一个Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED,这个Action只会发出一次。

2.构造一个IntentReceiver类,重构其抽象方法onReceiveIntent(Context context, Intent intent),在其中启动你想要启动的Service。

3.AndroidManifest.xml中,首先加入<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>来获得BOOT_COMPLETED的使用许可,然后注册前面重构的IntentReceiver类,在其<intent-filter>中加入<action android:name="android.intent.action.BOOT_COMPLETED" /> ,以使其能捕捉到这个Action。

一个例子
xml:

Java代码
  1. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>   
  2. <receiver android:name=".OlympicsReceiver" android:label="@string/app_name"   
  3.     <intent-filter>    
  4.        <action android:name="android.intent.action.BOOT_COMPLETED" />    
  5.        <category android:name="android.intent.category.LAUNCHER" />    
  6.     </intent-filter>    
  7. </receiver>  

 java

Java代码
  1. public class OlympicsReceiver extends IntentReceiver    
  2. {   
  3.       
  4.     static final String ACTION "android.intent.action.BOOT_COMPLETED";   
  5.            
  6.     public void onReceiveIntent(Context context, Intent intent)    
  7.     {   
  8.         if (intent.getAction().equals(ACTION))    
  9.         {   
  10.                   context.startService(new Intent(context,    
  11.                        OlympicsService.class), null);//启动倒计时服务   
  12.              Toast.makeText(context, "OlympicsReminder service has started!"Toast.LENGTH_LONG).show();   
  13.         }   
  14.     }   
  15.  

 注意:现在的IntentReceiver已经变为BroadcastReceiver,OnReceiveIntent为onReceive。所以java这边的代码为:

 

(也可以实现应用程序开机自动启动)
Java代码
  1. public class OlympicsReceiver extends BroadcastReceiver   
  2. {   
  3.       
  4.     static final String ACTION "android.intent.action.BOOT_COMPLETED";   
  5.            
  6.     public void onReceive(Context context, Intent intent)    
  7.     {   
  8.         if (intent.getAction().equals(ACTION))    
  9.         {   
  10.                   context.startService(new Intent(context,    
  11.                        OlympicsService.class), null);//启动倒计时服务   
  12.              Toast.makeText(context, "OlympicsReminder service has started!"Toast.LENGTH_LONG).show();   
  13.             //这边可以添加开机自动启动的应用程序代码   
  14.         }   
  15.     }   
  16. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中让程序开机自动启动,通常有两种方式: 1. 使用广播接收器 在 AndroidManifest.xml 文件中添加以下代码: ``` <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> ``` 然后创建一个 BroadcastReceiver,在接收到 `android.intent.action.BOOT_COMPLETED` 广播时启动你的程序。 示例代码: ```java public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { Intent bootIntent = new Intent(context, YourActivity.class); bootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(bootIntent); } } } ``` 在 AndroidManifest.xml 中添加以下代码: ```xml <receiver android:name=".BootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> ``` 2. 使用系统服务 在 AndroidManifest.xml 文件中添加以下代码: ``` <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> ``` 然后创建一个 Service,在 `onCreate()` 方法中启动你的程序。 示例代码: ```java public class BootService extends Service { @Override public void onCreate() { super.onCreate(); Intent bootIntent = new Intent(this, YourActivity.class); bootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(bootIntent); } @Override public IBinder onBind(Intent intent) { return null; } } ``` 在 AndroidManifest.xml 中添加以下代码: ```xml <service android:name=".BootService"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </service> ``` 注意:使用第二种方式需要在 Android 5.0 以上版本上进行测试,因为 Android 5.0 以下版本不支持在 Service启动 Activity。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值