2014-07-19 109 views

回答

0

你可以用它来制作它的工作:

manifest文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.jjoe64"> 

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

    <application> 
     <receiver android:name=".BootCompletedIntentReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 
     <service android:name=".BackgroundService" /> 
    </application> 

</manifest> 

添加该类BootCompletedIntentReceiver.java

public class BootCompletedIntentReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { 
      Intent pushIntent = new Intent(context, BackgroundService.class); 
      context.startService(pushIntent); 
     } 
    } 
} 

这将使得开机后自动启动的服务设备

+0

为什么在'BroadcastReceiver'中硬编码'Intent'?你为什么不使用相应的常量'Intent.ACTION_BOOT_COMPLETED'?此外缩进你的代码与4个空格,而不是一个...我这次已经修复你的格式。 –

0

除了Himans胡锦涛的回答是:

  • 一开机,监听应用不得安装在SD卡。请记住,设备引导可以完成 之前,SD卡甚至安装导致无法被激活

  • 引导监听不会被激活你的应用程序的启动监听,直到用户将激活你的 应用首次。从4.2开始(不确定)Android会阻止所有服务& 由新安装的应用程序声明的侦听器被激活,直到用户明确激活。

在用户单击主屏幕图标时显式激活。

相关问题