简体   繁体   中英

Simplest way to get to `BroadcastReceiver` with `AlarmManager` not working

Suppose I have a MainActivity.java , where if I press a button(id= get_button ), I will go to the onReceive() of NotificationReceiver.java 2 minutes later :

But I am not going there. As for this and this and many other resources I googled, This seems to be the right way.

My MainActivity.java :

package com.example.insanes.chothavandar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.util.Calendar;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.get_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                Intent intent = new Intent(MainActivity.this, NotificationReceiver.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(
                        MainActivity.this,
                        0,
                        intent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + 2);
                cal.set(Calendar.SECOND, 0);
                am.setRepeating(
                        AlarmManager.RTC_WAKEUP,
                        cal.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY,
                        pendingIntent);
            }
        });
    }
}

My NotificationReceiver.java :

package com.example.insanes.chothavandar;

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

public class NotificationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Not logging after 2 minutes
        // Am I doing something wrong?
        Log.d("DEBUG-EXISTENSE", "Reached in the broadcastreceiver");
    }
}

I have registered the receiver in the manifest:

My menifest.xml :

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

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <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=".NotificationReceiver"/>
    </application>

</manifest>

you want to write this code in your manifest file. ex :

android:name="com.example.insanes.chothavandar.NotificationReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.insanes.chothavandar.SHOW_NOTIFICATION" />
            </intent-filter>
        </receiver>

If you will go to the onReceive() of NotificationReceiver.java 2 minutes later so, you will use a handler in your class. ex:

 private Handler mStatusHandler = new Handler();
 private Runnable mStatusRunnable;
 private void checkStatus() {
        mStatusRunnable = new Runnable() {
        @Override
        public void run() {
            // Hear right your on button click code.
            checkStatus();
        }

    };
    mStatusHandler.postDelayed(mStatusRunnable, 2000);
}

i don't know about the right way or not , but this can be done via Handler in pretty simple way,

int TIME=2000
new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
               // do stuff
            }
        }, TIME);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM