Alarm receive don't work

爱⌒轻易说出口 提交于 2020-01-11 13:15:10

问题


I'm making an alarm receiver, but the alarm is not triggered.

This is my code :

Start alarm in MainActivity :

private void setupAlarm(){
    Intent intent = new Intent(this, com.logdata.AlarmReceiver.class);
    PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);       
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 1, 1000, pIntent);

    Log.e("setupAlarm", "Setup alarm complete");
}

Manifest :

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

package="com.phonelogger"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.WRITE_CALL_LOG"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity android:name="SettingActivity" android:label="@string/action_settings" ></activity>

    <service android:name="com.logdata.LogManager" />

    <receiver 
        android:name="com.logdata.AlarmReceiver" 
        android:enabled="true">                                    
    </receiver>
</application>

AlarmReceiver :

package com.logdata;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent myIntent = new Intent(context, com.logdata.LogManager.class);
        context.startService(myIntent);

        Log.e("AlarmReceiver", "Get message");      
    }

}

Can someone tell me what the problem is ?


回答1:


Your Intent is used to explicitly start the AlarmReceiver class which is a BroadcastReceiver. Hence, you need to use getBroadcast(), not getService(), to create the PendingIntent object.

Replace

PendingIntent pIntent = PendingIntent.getService(this, 0, intent, 0);       

with

PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);  

Try this. This will work.




回答2:


Try To Use THis. It will Work for you

private void setupAlarm(){
Intent intent = new Intent(this, com.logdata.AlarmReceiver.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);       
AlarmManager manager = (AlarmManager)  getSystemService(Context.ALARM_SERVICE);
 manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,SystemClock.elapsedRealtime() + 1, 1000, pIntent);

Log.e("setupAlarm", "Setup alarm complete");

}



来源:https://stackoverflow.com/questions/30705606/alarm-receive-dont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!