Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

android - Alarmmanager not working after phone reboot

I had created alarmmanager on button click. but it not working after phone reboot. my AlarmbroadcastReceiver not call on phone reboot. it work when phone lock , application killed but not work after phone reboot i had created one progressbar which start on button click and stop after alarm broadcast fired but it not stop when phone reboot. i had added my button click event and broadcast receiver class

Button click event

b1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
pb1.setVisibility(View.VISIBLE);
progress_edit.putBoolean("progress_one", true);
progress_edit.apply();
                    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                    Intent intnt = new Intent(getApplicationContext(), AlarmbroadcastReceiver.class);
                    intnt.setAction("com.ex.Alarm");
                    PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(), 0, intnt, 0);
                    manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 120000, pending);
                                Log.d("Broadcast ","Fired");
                }
            });

BroadcastReceiver Class

    @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("inside","broadcast receive");
            if(intent.getAction().equalsIgnoreCase("com.ex.Alarm"))
            {
enterSys_progress_edit.putBoolean("progress_one", false);
                enterSys_progress_edit.apply();
                Toast.makeText(context,"Receive",Toast.LENGTH_LONG).show();
            }

        }

My Manifest file

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.krutarth.alarm">
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            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=".AlarmbroadcastReceiver" android:enabled="true" android:exported="false" android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
                <intent-filter >
                    <action android:name="android.intent.action.BOOT_COMPLETED"/>
                </intent-filter>
            </receiver>
        </application>
    </manifest>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

all alarms are reset when phone restart ,So create a callback on reboot like this

public class BootCompletedIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
       ////// reset your alrarms here 
    }

}

}

ALSO ADD THIS TO YOUR MANIFEST TO REGISTER THE BROADCAST

<receiver android:name=".receiver.BootCompletedIntentReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
</receiver>

Create a method say,

setMyalarm(){
     // here set the alarm as u need
}

now call this method setMyAlarm ,whenever and wherever u need to set that particular alarm,whether its on a button click or whether it on a reboot reciever


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...