无法通过活动呼叫BroadcastReceiver

时间:2018-07-20 16:17:25

标签: android broadcastreceiver alarmmanager

我是android的新手,这里有两个活动和一个BroadcastReceiver。

我正在尝试从第一个活动到第二个活动调用BroadcastReceiver。

Manifest.xml

Identification_Number

WakeUpScreen.java

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

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

    <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=".Activity_Classes.MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity_Classes.AlarmActivity"
            android:screenOrientation="portrait" />
        <activity
            android:name=".Activity_Classes.CreateAlarm"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateAlwaysHidden" />
<activity android:name=".Activity_Classes.WakeUpScreen"></activity>
        <receiver
            android:name=".AlarmReceiver"
            android:enabled="true" />
    </application>

</manifest>

我正在尝试从唤醒屏幕停止警报,因为我在AlarmActivity中调用了方法来取消当前的未决

AlarmActivity.java

 mStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alarmActivity.stopAlarm(getBaseContext());
            }
        });

从这个方法(意图),我想调用BroadcastReceiver来停止(当前未决的警报)警报。

AlarmReceiver.java

   public void stopAlarm(Context context){
      Intent intent = new Intent(context, AlarmReceiver.class);
      intent.setAction("ALARM_OFF");
      PendingIntent pendingIntent = PendingIntent.getBroadcast(context,mAlarmId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
      alarmManager.cancel(pendingIntent);
  }

在调试从WakeUpScreen.java调用的应用程序stopAlarm(Context context)方法时,调试器也跨过了stopalarm()方法中的所有行,但没有调用AlarmReceiver。

谁能帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

AFAIK AlarmManager.cancel仅用于取消现有的PendingIntent任务。
并且它不会取消取消时的待定意图。

对于您而言,取消闹钟后,您必须以“ ALARM OFF”的额外意图手动发送广播。

 IntentFilter intentFilter = new IntentFilter();
 intentFilter.addAction("ALARM_ON");
 intentFilter.addAction("ALARM_OFF");
 LocalBroadcastManager.getInstance(this).registerReceiver(new AlarmReceiver(), 
 intentFilter);
 mStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alarmActivity.stopAlarm(getBaseContext());

                Intent intent = new Intent("ALARM_OFF");
                intent.setClass(WakeUpScreen.this, AlarmReceiver.class);
                LocalBroadcastManager.getInstance(WakeUpScreen.this).sendBroadcast(intent);

            }
        });