使用`AlarmManager`无法正常工作的最简单方法是使用`BroadcastReceiver`

时间:2017-11-30 04:16:59

标签: java android broadcastreceiver alarmmanager

假设我有MainActivity.java,如果按下按钮(id = get_button),我会在2分钟后转到onReceive() NotificationReceiver.java:< / p>

但我不会去那里。至于thisthis以及我搜索过的许多其他资源,这似乎是正确的方法。

我的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);
            }
        });
    }
}

我的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");
    }
}

我在清单中注册了接收器:

我的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>

2 个答案:

答案 0 :(得分:1)

您希望在清单文件中编写此代码。 例如:      

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

如果你将在2分钟后转到NotificationReceiver.java的onReceive(),那么你将在你的类中使用一个处理程序。 例如:

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

答案 1 :(得分:-1)

我不知道正确的方法,但这可以通过Handler以非常简单的方式完成,

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