Alarm receive don't work

176 views Asked by At

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 ?

2

There are 2 answers

0
Yash Sampat On BEST ANSWER

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.

1
Rishi Paul On

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");

}