My AlarmManager not getting called

為{幸葍}努か 提交于 2019-12-11 18:28:50

问题


I try every code which I got from Stackoverflow or from anywhere, but not getting success in AlarmManager, my BroadcastReceiver not even getting called. Here is my code:

    //My AlarmManager is in MyDB.Class extends SQLiteOpenHelper{

                System.out.println("Inside AlarmManager");
                // Create alarm manager
                AlarmManager alarmMgr0 = (AlarmManager) mContext
                        .getSystemService(Context.ALARM_SERVICE);

                System.out.println("Inside alarmMgr0     " + alarmMgr0);

                // Create pending intent & register it to your alarm notifier
                // class
                Intent intent0 = new Intent(mContext, AlarmReciever.class);
                intent0.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent0.putExtra("req_code", v); // pI);
                PendingIntent pendingIntent0 = PendingIntent.getBroadcast(
                        mContext, v, intent0, 0);
                System.out.println("Inside pendingIntent0     "
                        + pendingIntent0);

                // set timer you want alarm to work
                Calendar timeOff9 = Calendar.getInstance();
                timeOff9.add(Calendar.DAY_OF_YEAR, scheduleDao1.getMeetingDay());
                System.out.println("Inside if getDay    "
                        + scheduleDao1.getDay());
                timeOff9.set(Calendar.MONTH, scheduleDao1.getMeetingMonth());
                System.out.println("Inside if getMonth    "
                        + scheduleDao1.getMonth());
                timeOff9.set(Calendar.YEAR, scheduleDao1.getMeetingYear());
                System.out.println("Inside if getYear    "
                        + scheduleDao1.getYear());

                if (scheduleDao1.getFromTimeHr() != 00) {
                    System.out.println("Inside if getFromTimeHr    "
                            + (scheduleDao1.getFromTimeHr() - 1));

                    timeOff9.set(Calendar.HOUR_OF_DAY,
                            scheduleDao1.getFromTimeHr() - 1);
                }
                timeOff9.set(Calendar.MINUTE,
                        scheduleDao1.getFromTimeMin());
                System.out.println("Inside getMeetingFromTimeMin     "
                        + scheduleDao1.getFromTimeMin());
                timeOff9.set(Calendar.SECOND, 0);

                // set that timer as a RTC Wakeup to alarm manager object
                alarmMgr0.set(AlarmManager.RTC_WAKEUP,
                        timeOff9.getTimeInMillis(), pendingIntent0);

                contents.put(ALARM_REQUEST_CODE, v);

                //}

My Receiver class is here (Which is not get even called)

public class AlarmReciever extends BroadcastReceiver {
    Context mContext;
    int request_code = 0;

@Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        mContext = context;
        System.out.println("Inside onReceive");
        mContext = context;
        request_code = intent.getIntExtra("req_code", 0);
        System.out.println("Inside request_code       " + request_code);

//      Bundle extras = intent.getExtras();
//      request_code = extras.getInt("req_code");
//      System.out.println("Inside request_code       " + request_code);

      }
}

And my Manifest code is like that....

.
.
.
<receiver android:name=".AlarmReciever" 
            android:enabled="true"/>
    </application>

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

I didn't get what is going wrong with these code please someone help me here.


回答1:


Try the code below:

final static int RQS_1 = 1;
    public void setAlarm1(long interval){

            /* info.setText("\n\n***\n"
                + "Alarm1 is set@ " + targetCal.getTime() + "\n"
                + "***\n");*/

            Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),RQS_1, intent, 0);

            AlarmManager alarmManager1 = ((ApplicationConstant)ListOfAds.this.getApplication()).getmanager();

             alarmManager1 = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
            //alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);   
            alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP,interval, 60000, pendingIntent);

             System.out.println("Alarm fired ListofAds:"+System.currentTimeMillis());

        }

The AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

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

    Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
    }

Manifest.xml

 <receiver
            android:name="yourpackagename.AlarmReceiver"
            android:enabled="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
            android:process=":remote" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

Permissions:

  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />

Try the above code.I am sure that the Alarm Manager will get called.




回答2:


Finally i got What i want, with the help of @kgandroid & @rainash Here i post my Solve code now. which is working fine for me & Showing Notifications. And i also able to set here Multiple AlarmManager with my required time.

// Create alarm manager
   AlarmManager alarmMgr0 = (AlarmManager) mContext
		.getSystemService(Context.ALARM_SERVICE);
				
		// Create pending intent & register it to your alarm notifier
				// class
		Intent intent0 = new Intent("alarm");
		intent0.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent0.putExtra("req_code", v); // pI);
		PendingIntent pendingIntent0 = PendingIntent
				.getBroadcast(mContext, v, intent0,
						PendingIntent.FLAG_UPDATE_CURRENT);

		// set timer you want alarm to work
	Calendar timeOff9 = Calendar.getInstance();
	timeOff9.setTimeInMillis(System.currentTimeMillis());
	timeOff9.set(scheduleDao1.getYear(),
		(scheduleDao1.getMonth()) - 1,  //Months start from '0'
		scheduleDao1.getDay(),
	(scheduleDao1.getFromTimeHr()) - 1,//I want those notification before 1hr
			scheduleDao1.getFromTimeMin(), 0);

	// long dateWeGet = Long.valueOf(dateString);
   // set that timer as a RTC Wakeup to alarm manager object
		alarmMgr0.set(AlarmManager.RTC_WAKEUP,
				timeOff9.getTimeInMillis(), pendingIntent0);

contents.put(ALARM_REQUEST_CODE, v);


来源:https://stackoverflow.com/questions/32493754/my-alarmmanager-not-getting-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!