2

In my application I'm trying to call BroadcastReceiver using AlarmManager to call the service every minute. But for some reason it's not getting called. I can't figure out the reason for this. I'm writing this code by following a working project, and I couldn't find anything different. Please help.

AndroidManifest.xml

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

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.WRITE_CONTACTS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".activities.SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activities.MainActivity"/>
        <activity android:name=".activities.ServiceChecker"/>

        <service
            android:name="services.MessageSendingService"
            android:enabled="true"
            android:exported="true"/>

        <receiver android:name="broadcastReceiver.MessageServiceScheduler">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <receiver android:name="broadcastReceiver.MessageServiceTrigger"/>

        <receiver
            android:name="broadcastReceiver.SendBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="SMS_SENT" />
            </intent-filter>
        </receiver>

        <receiver
            android:name="broadcastReceiver.DeliveryBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="SMS_DELIVERED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

SplashScreen.java

public class SplashScreen extends AppCompatActivity {

    private Button mainUI;
    private TextView dateTime;
    private Button serviceChecker;
    private static final long REPEAT_TIME = 1000;

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

        this.initializeDatabase();
        this.triggerService();

        mainUI = (Button) findViewById(R.id.main_ui);
        dateTime = (TextView) findViewById(R.id.date_time);
        serviceChecker = (Button) findViewById(R.id.service);

        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        dateTime.setText(dateFormat.format(cal.getTime()));

        mainUI.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
            }
        });

        serviceChecker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(SplashScreen.this, ServiceChecker.class);
                startActivity(i);
            }
        });

    }

    public void initializeDatabase() {
        DBAdapter dbAdapter = new DBAdapter(this);
        dbAdapter.open();
        dbAdapter.close();
    }

    public void triggerService() {

        Intent intent = new Intent(this, MessageServiceTrigger.class);
        PendingIntent pending = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Calendar cal = Calendar.getInstance();
        alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, pending);

    }
}

MessageServiceTrigger.java

public class MessageServiceTrigger extends BroadcastReceiver {

    private static final String TAG = MessageServiceTrigger.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG,TAG+" onReceive get called");
    }
}
smac89
  • 39,374
  • 15
  • 132
  • 179
  • the documentation for setInexactRepeating() : As of API 19, all repeating alarms are inexact. Because this method has been available since API 3, your application can safely call it and be assured that it will get similar behavior on both current and older versions of Android. check this link : https://developer.android.com/reference/android/app/AlarmManager.html#setInexactRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29 – prGD Jul 14 '16 at 11:58

3 Answers3

0

First of all you need to change your REPEAT_TIME = 1000; This corresponds to 1 second, not one minute.

private static final long REPEAT_TIME = 60000;

Next try to change your triggerService() method as below.

public void triggerService() {
    Intent intent = new Intent(this, MessageServiceTrigger.class);
    PendingIntent pending = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), REPEAT_TIME, pending);
}
Marat
  • 6,142
  • 6
  • 39
  • 67
  • What do your get? App crashes? Or it works fine but only alarms are not getting called? If you receive errors, please include them into your question – Marat Jul 14 '16 at 12:00
  • That's the problem I'm not getting any errors. That Log message inside MessageServiceTrigger onReceive not get's display. – Chathuranga Shan Jayarathna Jul 14 '16 at 12:07
  • Could you include Log message inside of `triggerService()` method to know if app is running code in it? – Marat Jul 14 '16 at 12:27
  • No No, Trigger service method executes without problem. From that method MessageServiceTrigger class extending BroadcastReceiver not gets executed. That Log message inside onReceive method should printed in logcat. But it didn't show up. I even put break points and debug to make sure. – Chathuranga Shan Jayarathna Jul 14 '16 at 13:44
  • Check this link to see, if your alarm is indeed setup http://stackoverflow.com/questions/28742884/how-to-read-adb-shell-dumpsys-alarm-output. Also, for debugging purpose, increase the interval to 2 min or so and see if at least the trigger happens. – prashant Jul 14 '16 at 14:38
  • @ChathurangaShanJayarathna I have updated my answer please have a look at it. Try to repeat it exactly as it is – Marat Jul 14 '16 at 14:43
  • I think what ever the mistake I made in my 'MessageServiceTrigger' class Because I call it manually like this. 'Intent intent = new Intent("com.shan.chathuranga.smsscheduler.broadcastReceiver.MessageServiceTrigger"); sendBroadcast(intent);' This should show the log message inside 'onReceive' Right ? – Chathuranga Shan Jayarathna Jul 14 '16 at 15:34
-1

Try this

    <receiver android:name="broadcastReceiver.MessageServiceTrigger" 
              android:process=":remote" />
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
-1

Is there any rule that saying you shouldn't use capital letters for package names. Because that's what wasted my all day. Since I have another four broadcast receivers in my project I created a package with the name of broadcastReceiver. that's where I placed my MessageServiceTrigger class. I change that in to broadcast_receiver just because I don't have any other thing to change in my code and I have working project with me which have Identical code that working fine. Now it's working. Thanks for all your answers.