为什么BOOT_COMPLETED后服务无法启动?

时间:2015-12-31 11:15:17

标签: android service broadcastreceiver alarmmanager

我添加我的后台服务运行完美,但我的手机关闭然后打开然后我的reciver类在get动作android.intent.action.BOOT_COMPLETED.but服务不start.my代码下面请帮助我!!!

public class ReceiverCall extends BroadcastReceiver {
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";
    static final String ACTION1 = "android.intent.action.QUICKBOOT_POWERON";
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("log_tag", "Action :: "+intent.getAction());
         if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
                /* Setting the alarm here */
                Intent alarmIntent = new Intent(context, MyAlarmService.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
                AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), + (1000 * 60 * 2), pendingIntent);
                Log.v("log_tag", "REPEAT");
            }
    }

我还在表现中添加了许可。

 <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.employeemanager.app.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name="***8"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            android:name="***.MyAlarmService"
            android:enabled="true" >
        </service>

        <receiver android:name="***.ReceiverCall"
            android:enabled="true"  >
            <intent-filter>
                <action android:name="***" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
                <category android:name="android.intent.category.LAUNCHER" >
                </category>
            </intent-filter>
        </receiver>
    </application>

2 个答案:

答案 0 :(得分:3)

也许你输入错误,但如果你想让Intent工作,你应该致电:

context.startService(alarmIntent);

如果您的Logcat上显示此日志Log.v("log_tag", "Action :: "+intent.getAction());,请告诉我这个Log.v("log_tag", "REPEAT");也是如此。

我建议你的BroadcastReceiver将是这样的:

public class ReceiverCall extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.v("log_tag", "Action :: "+intent.getAction());
     if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            //make an intent to your Service as follows 
            Intent serviceIntent = new Intent(context, MyAlarmService.class);
            context.startService(serviceIntent);
        }
}

然后在Service onCreate Service的{​​{1}}添加此内容:

/* Setting the alarm here */
Intent alarmIntent = new Intent(context, Whatever.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), + (1000 * 60 * 2), pendingIntent);
Log.v("log_tag", "REPEAT");

我还建议您创建一个新的BroadcastReceiver并在其上执行您想要重复的内容,并将whatever.class替换为BroadccastReceiver

答案 1 :(得分:2)

您的应用程序安装在外部存储上的可能性很大,它无法接收启动完成的事件。您应该在清单中添加android:installLocation="internalOnly"