Android alarm not working

前端 未结 3 1998
北海茫月
北海茫月 2020-12-16 16:18

I\'ve been struggling with this for hours. I\'ve also checked the documentation and several topics. I found this code in two topics, both guys said the code was working perf

相关标签:
3条回答
  • 2020-12-16 16:58

    I had the same problem until I found that I had put my Broadcast Receiver on a different package, not the general.

    Simply changed:

    <receiver android:name=".AndroidAlarmService" android:enabled="true" >
    

    for:

    <receiver android:name="com.MyCompany.MyPackage.AndroidAlarmService" android:enabled="true" >
    
    0 讨论(0)
  • 2020-12-16 17:10

    Actually you dont need to specify the action since you use the class AlarmReceiver.class in the intent.

    In your AndroidManifest.xml, make sure you have a receiver definition within the <application> tags, something like:

    <receiver android:name="AlarmReceiver">

    Edit: Ok there are 2 ways to use your broadcast receiver.

    1) From the code you have provided, AlarmReceiver.java that will contains:

    public final class AlarmReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
    
        }
     }
    

    and HelloAndroid2.java:

    public class HelloAndroid2 extends Activity {  
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);   
    
        Intent intent = new Intent(this, AlarmReceiver.class);
    
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_ONE_SHOT);
    
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
        Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
    
    }
    
    }
    

    Like this, you can set your broadcast receiver to work with the AndroidManifest.xml and the tag <receiver ...>

    2)2nd way. With this way, you can use just 1 file HelloWorld2.java:

    In your activity, create your broadcast receiver and register it.

    public class HelloWorld2 extends Activity {
        private SharedPreferences prefs;
        private String mName;
    
    
        BroadcastReceiver alarmReceiver = new BroadcastReceiver() {
    
            @Override
            public void onReceive(Context context, Intent intent) {
                Toast.makeText(context, "Alarm worked", Toast.LENGTH_LONG).show();          
            }
        };
    
    
        public static final String ACTION_NAME = "com.helloworld.MYACTION";
        private IntentFilter myFilter = new IntentFilter(ACTION_NAME);
    
    
        @Override
        protected void onPause() {
            unregisterReceiver(alarmReceiver);
            super.onPause();
        }
    
        @Override
        protected void onResume() {
            registerReceiver(alarmReceiver, myFilter);
            super.onResume();
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
    
            registerReceiver(alarmReceiver, myFilter);
    
            Intent intent = new Intent(ACTION_NAME);        
    
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
            intent, PendingIntent.FLAG_ONE_SHOT);
    
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10 * 1000), pendingIntent);
            Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
    
    
        }
    
    0 讨论(0)
  • 2020-12-16 17:19

    If the answer above doesn't work for you then there is another way to not receive any callbacks when AlarmManager fires an expired alarm. You simply need to check this one out: by sending the wrong Intent on instantiation of PendingIntent. For example you wanted to receive a call onReceive on one of your receivers but you instantiated a PendingIntent via getActivity or getService, but what you actually meant is getReceiver.

    When creating instance of PendingIntent, there are many ways to create it (getService, getActivity,getReceiver, getForegroundService:

    if you want Activity the receiver of the intent then you:

    PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*);
    

    if you want BroadcastReceiver the receiver of the intent:

    PendingIntent.getReceiver(this, 0, intent, PendingIntent.FLAG_*);
    

    if you want a foreground Service the receiver of the intent:

    PendingIntent.getForegroundService(this, 0, intent, PendingIntent.FLAG_*);
    

    if you want a Service the receiver of the intent:

    PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_*);
    

    Also, make sure you intents are pointing to the correct class. (e.g. creating intents for Activity, Service etc.). You will not receive any call if you pass wrongfully like this:

    Intent intent = new Intent(this, MyReceiver.class); // You wanted receiver
    
    // PendingIntent was created in such a way 
    // you wanted this to be received by an activity. 
    // you will not receive any call if you set it up like this.
    PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*); 
    

    I also posted similar answer here.

    HTH

    0 讨论(0)
提交回复
热议问题