1

我将自动启动编码为:

public class AutoStart extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            SetAlarm(context);
        }       

    }

    public void SetAlarm(Context context) {
        AlarmManager am = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(context, ServiceUpdateData.class);
        // PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
        PendingIntent pi = PendingIntent.getService(context, 0, i,
                PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 30);// 
        int dbtime = 10;

        int time = 1000 * 60 * dbtime;
        am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                time, pi); // Millisec * Second * Minute
        Toast.makeText(context, "Service Started Again", Toast.LENGTH_LONG)
                .show();
    }
}

我在 androidmainifest.xml 中添加了

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

为什么我的服务在启动设备 android 后没有自动启动。你能帮助我吗?

4

2 回答 2

1

不要忘记设置Intent-FilterBOOT_COMPLETED

喜欢:

<receiver android:name=".AutoStart">
    <intent-filter>
        <action
            android:name ="android.intent.action.BOOT_COMPLETED">
        </action>
    </intent-filter>
</receiver>
于 2012-09-07T11:19:09.727 回答
1
   <receiver android:name="yourpackageName.classname"

        <intent-filter>
           <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>
于 2012-09-07T11:22:08.467 回答