How do you start an Activity with AlarmManager in Android?

前端 未结 8 918
鱼传尺愫
鱼传尺愫 2020-12-01 17:01

I\'ve poured through a dozen tutorials and forum answers about this problem, but still haven\'t been able to get some working code together. I\'ll try to keep the question s

相关标签:
8条回答
  • 2020-12-01 17:16

    In my experience you can achieve this without broadcast receiver, just use PendingIntent.getActivity() instead of getbroadcast()

    private void setReminder(){
    
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Calendar startTime = Calendar.getInstance();
                startTime.add(Calendar.MINUTE, 1);
                Intent intent = new Intent(ReminderActivity.this, ReminderActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                PendingIntent pendingIntent = PendingIntent.getActivity(ReminderActivity.this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                alarmManager.set(AlarmManager.RTC, startTime.getTimeInMillis(), pendingIntent);   
    }
    

    I've tested this code on android O but I'm not sure about other android versions please inform me if this doesn't work on any other android version.

    0 讨论(0)
  • 2020-12-01 17:17

    add this in your android mainifest file and it will hopefully work

    <activity android:name=".MyReceiver" />
            <receiver android:name=".MyReceiver"> </receiver>
    
    0 讨论(0)
  • 2020-12-01 17:17

    I had this problem too long ago to know which answer is correct, but thank you to everyone for their responses. I'm self-answering so the question isn't still open.

    0 讨论(0)
  • 2020-12-01 17:22

    Main Problem : if you close completely you're app and expect to start you're activity after 3 seconds, you wrong. because when you close you're app , you're app cant receive broadcast, for solve this problem use services instead of broadcasts.

    Point: when you're service would ran ,you cant start your activity if your app wouldn't in foreground.

    Solution: I think when your service started you can again set Alarmmanager to start your activity with PendingIntent for just now.

    Remember :

    1. When you create your intent for pass it to pendingIntent add the FLAG_ACTIVITY_NEW_TASK to it.
    2. For this PendingIntent use PendingIntent.getActivity() method and for the first PendingIntent use PendingIntent.getService() method.

    I hope this help you.

    0 讨论(0)
  • 2020-12-01 17:24

    In case someone else stumbles upon this - here's some working code (Tested on 2.3.3 emulator):

    public final void setAlarm(int seconds) {
        // create the pending intent
        Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
        // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,
                intent, 0);
        // get the alarm manager, and scedule an alarm that calls the receiver
        ((AlarmManager) getSystemService(ALARM_SERVICE)).set(
                AlarmManager.RTC, System.currentTimeMillis() + seconds
                        * 1000, pendingIntent);
        Toast.makeText(MainActivity.this, "Timer set to " + seconds + " seconds.",
                Toast.LENGTH_SHORT).show();
    }
    
    public static class AlarmReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
            Log.d("-", "Receiver3");
        }
    }
    

    AndroidManifest.xml:

        <receiver android:name="com.example.test.MainActivity$AlarmReceiver" >
        </receiver>
    

    Issues with BenLambell's code :

    • EITHER:
      • Move the receiver to it's own .java file or
      • make the inner class static - so it can be accessed from outside
    • Receiver is not declared correctly in the manifest:
      • if it's an inner class in MainActivity use: <receiver android:name="package.name.MainActivity$AlarmReceiver" ></receiver>
      • if it's in a separate file: <receiver android:name="package.name.AlarmReceiver" ></receiver>

    If your intention is to display a dialog in the receiver's onReceive (like me): that's not allowed - only activities can start dialogs. This can be achieved with a dialog activity.

    You can directly call an activity with the AlarmManager:

    Intent intent = new Intent(MainActivity.this, TriggeredActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    ((AlarmManager) getSystemService(ALARM_SERVICE)).set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + seconds * 1000, pendingIntent);
    
    0 讨论(0)
  • 2020-12-01 17:27

    According to Java convention class name begin with Capital letter.So change your

    "myReceiver" to "MyReceiver" and  "myActivity" to "MyActivity".
    

    Then add your receiver in the manifest file like the below.

    <application 
    ------------
    <receiver android:name="MyReceiver"></receiver>
    ---------------------
    </application>
    
    0 讨论(0)
提交回复
热议问题