为什么我的WakefulBroadcastReceiver onReceive没有被调用

时间:2016-08-27 12:23:51

标签: android android-alarms

我已经为AlarmManager编写了一个小例子。但我的WakefulBroadcastReceiver onReceive根本没有被调用。以下是我的代码,你能不知道为什么它没有被调用?

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wearable.wearables">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <receiver android:name="com.wearable.wearables.AlarmReceiver" />

        <activity
            android:name=".AlarmActivity"
            android:label="@string/title_activity_alarm"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

WakefulBroadcastReceiver

public class AlarmReceiver extends WakefulBroadcastReceiver {
    private static final String TAG = "AlarmReceiver";
    AlarmManager alarmManager;
    PendingIntent alarmIntent;
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG,"AlarmReceiver OnReceive called......");
        Intent service = new Intent(context, SchedulingService.class);
        startWakefulService(context,service);
    }

    public void setAlarm(Context context) {
        Log.d(TAG,"setAlarm called...");
        alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, AlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                System.currentTimeMillis() + 30000,
                60000,
                alarmIntent);
    }
}

活动设置闹钟

public class AlarmActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_alarm);

        AlarmReceiver alarmReceiver = new AlarmReceiver();
        alarmReceiver.setAlarm(this);

    }

}

setAlarm方法正在调用。我的意图是我需要每隔30秒调用一次wakefulService。问题是

  1. 代码不允许设置30秒(最少只允许一分钟)
  2. 根本没有调用WakefulBroadcastReceiver onReceive()..
  3. 我正在努力解决这个问题。有人可以帮我解决这个问题吗?

0 个答案:

没有答案