简体   繁体   中英

Broadcast Reveiver not being called from Alarm Manager

I've searched the archives and still can't resolve my issue.

I'm trying to take an user input in seconds that will call the broadcast receiver to create a toast message and vibrate the phone. However, the broadcast receiver is never called and I can't figure it out. My code is below.

Thanks!

package com.example.cs984x.alrm;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    Button btn1 = (Button) findViewById(R.id.button1);
    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
         public void onClick(View v) {
            EditText inputSeconds = (EditText) findViewById(R.id.editText1);
            int inputTime = Integer.parseInt(inputSeconds.getText().toString());
            Toast.makeText(MainActivity.this, "The alarm will go off in " + inputTime + " seconds.", Toast.LENGTH_LONG).show();
            AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
            myIntent.setAction("com.example.cs984x.alrm.vibrate");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
            alarmManager.set(AlarmManager.RTC_WAKEUP,   System.currentTimeMillis()+(inputTime*1000), pendingIntent);


        }
    });
}
}

Broadcast Receiver Class:

package com.example.cs984x.alrm;

import android.app.AlarmManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.widget.EditText;
import android.widget.Toast;

import static android.content.Context.ALARM_SERVICE;


public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "Your time is up", Toast.LENGTH_LONG).show();
    Vibrator v;
    v= (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
    v.vibrate(2000);

}

}

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cs984x.alrm">
<uses-permission android:name="android.permission.VIBRATE" >
</uses-permission>

    <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>
    </application>


    <receiver

    android:name=".MyReceiver" android:process=":remote"

    android:enabled="true">

  <intent-filter>

        <action android:name="com.example.cs984x.alrm.vibrate" />

    </intent-filter>

</receiver>


</manifest>

Add action to intent:

Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
myIntent.setAction("YourPackageName.YourAction");
            PendingIntent pendingIntent =PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

in AndroidManifest:

<intent-filter>
                <action android:name="YourPackageName.YourAction" />
            </intent-filter>

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