WakefulBroadcastReceiver无法在Android 6.0上运行

时间:2016-12-23 13:21:29

标签: java android broadcastreceiver background-task

我想在我的Android应用程序中实现后台任务。对于Android 5.0及更低版本,我的代码足以使用简单的BroadcastReceiver,我可以使用

轻松设置重复闹钟
setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * seconds, pendingIntent);

在Android 6.0上,我发现用 WakefulBroadcastReceiver BroadcastReceiver >

setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pendingIntent);

它仍然没有工作,我不知道我的错误是什么..

这是我的代码:

    public class AlarmManagerBroadcastReceiver extends WakefulBroadcastReceiver {

       @Override
       public void onReceive(Context context, Intent intent) {

           mContext = context;

           PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
           PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
           //Acquire the lock
           wl.acquire();

           callback = new GPSActivityCallback() {...}
           if(isConnectedToInternet()) {
              gpsService = new GPSService(callback, context);
              gpsService.refreshLocation();
              Bundle extras = intent.getExtras();
              StringBuilder msgStr = new StringBuilder();

              if (extras != null && extras.getBoolean(REPEAT, Boolean.FALSE)) {
                 //Make sure this intent has been sent by the one-time timer button.
              }
              Format formatter = new SimpleDateFormat("hh:mm:ss a");
              msgStr.append(formatter.format(new Date()));
           }
           else {
              Toast.makeText(context, "Internetverbindung wurde unterbrochen", Toast.LENGTH_SHORT).show();
              Log.d("INTERNET_TAG","Internetverbindung war kurrzeitig getrennt");
           }
           wl.release();

        public void SetAlarm(Context context) {
            AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
            intent.putExtra(REPEAT, Boolean.FALSE);
            PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

            if(Build.VERSION.SDK_INT < 23){
                if(Build.VERSION.SDK_INT >= 19){
                    am.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
                }
                else{
                    am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
                }
            }
            else{
                am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
            }
            am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 10, pi);
        }

        public boolean isConnectedToInternet(){...}
     }

这是我的清单:

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

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <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/Theme.AppCompat">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.nymvno.hiob.prototyp_v30.AlarmManagerBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="alarm" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

0 个答案:

没有答案