为什么我的闹钟没被执行?

时间:2016-12-14 13:39:36

标签: android service alarmmanager alarm

我有一个Activity应该通过Service启动BroadcastReceiver

三者概述:

Activity = Tracking
Service = PhoneManagementService
BroadcastReceiver = TrackingRestart 

ActivityAlarmManager方法中启动onCreate()

public class Tracking extends Activity {
private static final int ALARM_FREQUENCY_SECONDS = 60;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        startService(new Intent(Tracking.this, PhoneManagementService.class));
        Intent alarm = new Intent(this, TrackingRestart.class);
        boolean alarmRunning = (PendingIntent.getBroadcast(this, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
        if(!alarmRunning) {
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarm, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), (ALARM_FREQUENCY_SECONDS * 1000), pendingIntent);
        }
    }

我在其中加入startService(new Intent(Tracking.this, PhoneManagementService.class));,因为这是唯一一次调用Service

BroadcastReceiver看起来像这样:

public class TrackingRestart extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context, PhoneManagementService.class));
        context.stopService(new Intent(context, PhoneManagementService.class));
    }
}

Service类看起来像这样:

public class PhoneManagementService extends Service {

    private String user = "Service";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onCreate() {

        startForeground(NotificationCreator.getNotificationId(), NotificationCreator.getNotification(this));

        DBAdapter newEntry = new DBAdapter(this);
        newEntry.open();
        newEntry.addEntry("charging", user, getChargerType());
        newEntry.addEntry("battery", user, getBatteryStats());
        newEntry.addEntry("network", user, getNetworkType());
        newEntry.close();
    }
}

Manifest包含这三个条目:

    <activity android:name=".Tracking">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:name=".GPSService"
        android:process=":trackingService_gps"
        android:launchMode="singleTop"
        android:enabled="true" />
    <service
        android:name=".PhoneManagementService"
        android:process=":trackingService_phoneManagement"
        android:launchMode="singleTop"
        android:enabled="true" />
    <receiver
        android:name=".TrackingRestart"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="ch.jpabig.jonastrackingapp.ActivityRecognition.RestartTracking"/>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
            <action android:name="android.intent.action.BATTERY_LOW"/>
            <action android:name="android.intent.action.BATTERY_OKAY"/>

        </intent-filter>
    </receiver>

不知何故,数据库中的条目没有被制作出来,而且我很遗憾为什么会这样。一定是警报,但我可能是非常错误的。

0 个答案:

没有答案