I'm writing an Android app that has a list of deadlines and I want to notify to user, when the time has come, that one of them is expired at the exact deadline's date, even if the app is not running.
I use an AlarmManager, a custom BroadcastReceiver and a custom Services class, but even if I give the exact time to the alarm it always fire notification only when I run the app.
This is where I set the alarm:
public static void setAlarm( String string){
AlarmManager alarms =(AlarmManager)Utility.getContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ALARM_INTENT_FILTER);
intent.putExtra("content",string);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Utility.getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND,00);
calendar.set(Calendar.MILLISECOND,00);
calendar.set(Calendar.AM_PM,Calendar.PM);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
This is the BroadcastReceiver:
public class NotificationReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
Intent service1 = new Intent(context, NotificationServices.class);
service1.putExtra("content",intent.getStringExtra("content"));
context.startService(service1);
}
}
This is the services class:
public class NotificationServices extends Service
{
#Override
public IBinder onBind(Intent arg0) {return null;}
#Override
public void onCreate() {super.onCreate();}
#SuppressWarnings("static-access")
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
JSONObject jsonObject = Utility.jsonObjectFromString(intent.getStringExtra("content"));
DeadLinesList deadlinesList = new DeadLinesList(jsonObject);
Date currentdate = Utility.removeTime(new Date());
String message = "";
for (DeadLine deadline : deadlineList.getDeadline()){
if (deadline.getDateDeadline()!= null && Utility.removeTime(deadline.getDateDeadline()).compareTo(currentdate) == 0){
message += deadline.getDeadlineName()+"\n";
}
}
if (message.length()>0){
NotificationCompat.Builder b = new NotificationCompat.Builder(Utility.getContext());
Intent newIntent = new Intent(Utility.getContext(), MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(Utility.getContext(),0, newIntent, 0);
b.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Message")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentIntent(pIntent)
.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND).build();
NotificationManager notificationManager = (NotificationManager) Utility.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, b.build());
}
return START_NOT_STICKY;
}
#Override
public void onDestroy() {super.onDestroy();}
}
This is the manifest where I register the services and the receiver:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.alarm.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:largeHeap="true">
<receiver android:name=".Notifications.NotificationReceiver">
<intent-filter>
<action android:name="ALARM_ACTION"/>
</intent-filter>
</receiver>
<service android:name=".Notifications.NotificationServices"
android:enabled="true">
</service>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|orientation|screenSize">
<meta-data
android:name="android.app.default_searchable"
android:value=".MainActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Where is the error? Why notification are fired only when I open app, and not at the precise time of the day, or when app is not running?
From the API of AlarmManager
Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.
Related
I'm relatively new to Android so I'm quite puzzled here...
I want a notification to be triggered at a certain time on a daily basis.
I use a Calendar class to set the time when the notification is triggered every day, and AlarmManager to set the repeating task.
The problem is, the notification is triggered when the app is initially launched and not at the pre-defined time configured in the Calendar object. When I run in debug mode, I see that I go to the setExact method.
I also created a receiver to handle the notification itself and open the MainActivity when the notification is clicked.
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
.
.
.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 0);
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
if (Build.VERSION.SDK_INT < 19)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
else
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
NotificationReceiver.java
public class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mainIntent = new Intent(context, MainActivity.class);
mainIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(context);
builder.setContentTitle("This is the title")
.setContentText("This is the text")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.deal_icon);
Notification notification = builder.build();
notification.defaults = Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
manager.notify(100, notification);
}
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.goldbox.goldboxdeals">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.android.alarm.permission.SET_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:name="com.sample.sample1.ApplicationHelper"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".NotificationReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
Calendar.getInstance() returns a calendar at your current local time. You're then setting the time to be what you wish, with today's date. When you schedule an alarm, the date is likely in the past. The system then will trigger the alarm immediately.
I believe you should use setRepeating(). You can set the trigger / interval times. Keep in mind that your app, or even the device itself, isn't necessarily running all the time.
I have two issues of Alarm manager. One is i want to repeat alarm every 10 seconds and other is my Broadcast Receiver is invoking after 10 second.
When i use Activity instead of Broadcast Receiver it is working But Broadcast Receiver is not working. And time is not proper some time it works in 1 min and some times it works i 50 Seconds. But i need every 10 Second.
My code is:
buttonStart.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
long tenMin = 1000/5 ;//here is 30 seconds
Intent intent = new Intent(MainActivity.this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,
0, intent, 0);
AlarmManager am =
(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), tenMin, pendingIntent);
}});
BoradcastReceiver.java
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
}
}
Manifest file:
<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>
<service android:name=".MyAlarmService" />
<receiver
android:name=".MyBroadcastReceiver"
android:process=":remote"></receiver>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
Use:
Activity:
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,0, intent, 0);
10sec repeating interval,
repeatingInterval = 10 *1000; //interval in milli seconds for 10sec
AlarmManager alarmManager =(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
alarmManager .setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), repeatingInterval , pendingIntent);
It should work.
I'm trying to trigger a notification every 15 minutes, starting from the time I set. Currently my app notifies only when my application is open or when it is in the recent applications but when i closed my application the notification doesn't work any longer but when i open again my app it will trigger the notification. Based on my research I need a Service. So I have 3 files.
In my MainActivity, i set the alarm
private void scheduleNotification(int week){
Intent notificationIntent = new Intent(this, NotificationPublisher.class);
notificationIntent.putExtra("notifId", getResources().getInteger(R.integer.notification_id));
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, getResources().getInteger(R.integer.notification_id), notificationIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, week);
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
}
in the MainActivity i call a BroadcastReceiver which is the NotificationPublisher. Here is my NotificationPublisher
public class NotificationPublisher extends BroadcastReceiver {
final String TAG = "NotificationPublisher";
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, NotificationAlarmService.class);
service.putExtra("notifId", intent.getIntExtra("notifId", 0));
context.startService(service);
}
then I start a service by using the BroadcastReceiver which is the NotificationAlarmService, here i build a Notification using NotificationCompat.Builder.
public class NotificationAlarmService extends Service {
. . . .
#Override
public int onStartCommand(Intent intent,int flag, int startId)
{
notificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, MainActivity.class);
pendingIntent = PendingIntent.getActivity(this, intent.getIntExtra("notifId", 0), mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("App");
builder.setContentText("Notify Me");
builder.setAutoCancel(true);
builder.setSmallIcon(getNotificationIcon());
builder.setContentIntent(pendingIntent);
notificationManager.notify(intent.getIntExtra("notifId", 0), builder.build());
return super.onStartCommand(intent, flag, startId);
}
I already tried returning START_STICKY instead of super.onStartCommand I understand that when i used a service it will run in the background even if the user closed the application. I also tried changing this and used getBaseContext() in the service but this it doesn't trigger the notification. I don't know if i'm missing something. Thank you in advance.
EDIT
Here is my Manifest
<receiver android:name=".NotificationPublisher"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".NotificationAlarmService"
android:enabled="true">
</service>
After a week, I finally got it. In my Manifest i put something like this
<receiver android:name=".NotificationPublisher">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name=".NotificationAlarmService"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="NOTIFICATION_SERVICE" />
</intent-filter>
</service>
i've removed the enabled="true" in the receiver and also add QUICKBOOT_POWERON. Then add an action in the service which is NOTIFICATION_SERVICE and call it in my service like this
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Try adding Service in your Manifest file (Same as declaring an Activity) under application tag like this,
<service android:name="com.example.android.NotificationAlarmService">
</service>
I am almost done, but when clicking the set Alarm button to fire the notification after 5 seconds for example, nothing happens.
this is the OnClick method for SetAlarm
public void setAlarm(View view) {
long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
Intent alertIntent = new Intent(this, AlertReceiver.class);
//schedule to happen at later date
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
and this is my receiver:
public class AlertReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
createNotification(context, "Times up", "5 seconds has passed", "Alert");
}
public void createNotification(Context context, String msg, String msgText, String msgAlert) {
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder=new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.not)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
//intent to fire when notification clicked on
mBuilder.setContentIntent(notificIntent);
//how the person will be notified
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
//cancel notification when clicked in the taskbar
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager= (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1,mBuilder.build());
}
and this is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yahyazeid.testnotification" >
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<service android:name=".AlertReceiver">
<intent-filter> <action android:name="NOTIFICATION_SERVICE" />
</intent-filter>
</service>
<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>
<activity android:name=".MoreInfoNotification"
android:label="More on notification"
android:parentActivityName=".MainActivity">
</activity>
</application>
</manifest>
Any suggestions?
The corrected manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yahyazeid.testnotification" >
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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>
<activity android:name=".MoreInfoNotification"
android:label="More on notification"
android:parentActivityName=".MainActivity">
</activity>
<service android:name=".AlertReceiver">
</service>
</application>
</manifest>
Try this code. It looks like you did not set the notification in your receiver.
public void scheduleAlarm(View V)
{
// time at which alarm will be scheduled here alarm is scheduled at 1 day from current time,
// we fetch the current time in milliseconds and added 1 day time
// i.e. 24*60*60*1000= 86,400,000 milliseconds in a day
Long time = new GregorianCalendar().getTimeInMillis()+5*1000;
// create an Intent and set the class which will execute when Alarm triggers, here we have
// given AlarmReciever in the Intent, the onRecieve() method of this class will execute when
Intent intentAlarm = new Intent(this, AlarmReciever.class);
//Get the Alarm Service
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
Toast.makeText(this, "Alarm Set for 5 seconds", Toast.LENGTH_LONG).show();
Then for the Receiver
public class Receiver extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.depressiontest)
.setContentTitle("Time has Expired")
.setContentText("5 Seconds has passed.")
.setVibrate(pattern)
.setAutoCancel(true);
mBuilder.setContentIntent(pi);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
And then in your manifest
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<service android:name=".Receiver">
<intent-filter> <action android:name="NOTIFICATION_SERVICE" />
</intent- filter>
</service>
For setting a specific date using the get Calendar instance
Calendar cal = Calendar.getInstance();
//Setting date for 11/29/2016
cal.set(Calendar.DATE,29); //1-31 days of week
cal.set(Calendar.MONTH,10); //January starts at 0 (zero), December = 11
cal.set(Calendar.YEAR,2016);//year...
//Setting the specific hour/min/sec
cal.set(Calendar.HOUR_OF_DAY, 16); //HOUR
cal.set(Calendar.MINUTE, 39); //MIN
cal.set(Calendar.SECOND, 10); //SEC
// Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getService(MainActivity.this, 0,intent, 0);
//or if you start an Activity
//PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0,intent, 0);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
In my project, I'm using alarm code. There is no error, but anything within the BroadcastReceiver class is not being executed. I don't understand where the fault is.
Im doing it in Windows using Eclipse. I also have specified the <receiver> class in AndroidManifest.xml.
I want the code whithin the BroadcastReceiver class to be executed. In this case, I want the text given whithin the receiver class to be displayed at the specified time.
This is my receiver class:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.registerReceiver(null, null);
Toast.makeText(context, "Time is
up!!!!.",Toast.LENGTH_LONG).show();
}}
Can anyone suggest a way to get rid of this problem?
Thanks!!!
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.project.rappel"
android:versionCode="1"
android:versionName="1.0">
<application
android:icon="#drawable/icon"
android:label="#string/app_name">
<provider
android:name="ScheduleProvider"
android:authorities="com.project.rappel" />
<activity
android:name=".Rappel"
android:label="#string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SetSchedule"></activity>
<activity
android:name=".DaysAndTimes"></activity>
<activity
android:name=".Tts"></activity>
<receiver
android:name="MyBroadcastReceiver"
android:process=":remote" />
</application>
<uses-sdk
android:minSdkVersion="8" />
</manifest>
Above is my androidmanifest.xml.This is the code I used for trigerring the Receiver.
public void startAlert(View view) {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",
Toast.LENGTH_LONG).show();
}
Looking only at this piece code it is not easy to answer.
However, it is likely that you have not specified the intent filter for the receiver in the manifest.
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="xyz.abc.YOUR_ACTION" />
</intent-filter>
</receiver>
The activity or service that is using the alarm, when "the time is up" has to send an Intent matching the action specified in the android manifest. You have to do something like this:
Intent MyIntent = new Intent("xyz.abc.YOUR_ACTION");
Context cont = this.getBaseContext();
cont.sendBroadcast(SMSIntent);
Hope this helps.
Cheers
I think the problem is in manifest file.
Try to remove this parameter "android:process=":remote"" from your reciever.
EDIT:
Please, see my code bellow it work's perfectly. Hope, it will help.
// registration
Intent intent = new Intent(context, AlarmReceiver.class).putExtra(AlarmReceiver.ALARM_ID, alarm.getId());
intent.setData(Uri.parse("date:" + alarm.getId()));
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // try to change last parameter like mine
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTime().getTime(), pendingIntent);
// in manifest in application tag
<receiver android:name=".ui.more.AlarmReceiver"/>
// receiver class
public class AlarmReceiver extends BroadcastReceiver {
public static final String ALARM_ID = "alarmId";
public void onReceive(Context context, Intent intent) {
context.startActivity(new Intent(context, StartAlarmActivity.class)
.putExtra(StartAlarmActivity.ALARM_ID, intent.getExtras().getInt(ALARM_ID))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
}
First try adding a period in your manifest file in front of MyBroadcastReceiver. It should look like: android:name=".MyBroadcastReceiver".
If that doesn't work (though hopefully it does) try adding 'Context.' before ALARM_SERVICE on this line:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
so it would look like:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Otherwise, I am not seeing anything immediately obvious.
This code works fine:
public void setupAlarm() {
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, 0);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
// Set Alarm for next 10 seconds
time.add(Calendar.SECOND, 10);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),
pendingIntent);
}
and in the androidmanifest.xml I used this code:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".MyAlarmReceiver" />
<activity
android:name=".AlarmSampleActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Thanks all for helping!!!