Reputation: 189
I am new to Android. I have tried an sample application for status bar reminder for mentioning date. But it is not working in Emulator, i don't know what i am missing. I have attached the sample code here
Main.java
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, 3);
cal.set(Calendar.YEAR, 2012);
cal.set(Calendar.DAY_OF_MONTH, 25);
cal.set(Calendar.HOUR_OF_DAY, 22);
cal.set(Calendar.MINUTE, 8);
cal.set(Calendar.SECOND,0);
Intent alarmintent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(), 1,alarmintent,PendingIntent.FLAG_UPDATE_CURRENT| Intent.FILL_IN_DATA);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
In the alarm receiving class i have the following code
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
NotificationManager nm;
public void onReceive(Context context, Intent intent) {
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Raja";
CharSequence message = "My First App...";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher,"Crazy About Android...", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
}
}
In the manifest file i have added the following line (before the application tag close) for Alarm receiving class
<receiver android:name=".AlarmReceiver"></receiver>
Log cat entries
03-28 00:11:18.411: I/ActivityManager(69): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.raja.sample/.Main } from pid 556
03-28 00:11:18.481: I/ActivityManager(69): Start proc com.raja.sample for activity com.raja.sample/.Main: pid=564 uid=10036 gids={}
03-28 00:11:18.491: D/AndroidRuntime(556): Shutting down VM
03-28 00:11:18.512: D/dalvikvm(556): GC_CONCURRENT freed 102K, 69% free 319K/1024K, external 0K/0K, paused 1ms+1ms
03-28 00:11:18.552: D/dalvikvm(556): Debugger has detached; object registry had 1 entries
03-28 00:11:18.571: I/AndroidRuntime(556): NOTE: attach of thread 'Binder Thread #3' failed
03-28 00:11:19.352: I/IMFO(564): RAKAAAAAAAAAAAAA
03-28 00:11:19.611: I/ActivityManager(69): Displayed com.raja.sample/.Main: +1s147ms
03-28 00:11:20.101: W/InputManagerService(69): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@4061d080 (uid=10035 pid=532)
03-28 00:11:24.751: D/dalvikvm(247): GC_EXPLICIT freed 7K, 54% free 2544K/5511K, external 1625K/2137K, paused 97ms
03-28 00:13:28.577: D/SntpClient(69): request time failed: java.net.SocketException: Address family not supported by protocol
Upvotes: 1
Views: 2117
Reputation: 14058
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(), 0);
You are passing a blank Intent here . Change this to the intent of Activity you want to all, for example :
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context,MyActivity.class), 0);
Try the following Activity:
public class TestAlarmActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get a Calendar object with current time
Calendar cal = Calendar.getInstance();
// add 5 minutes to the calendar object
cal.add(Calendar.MINUTE, 5);
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("alarm_message", "O'Doyle Rules!");
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender = PendingIntent.getBroadcast(this, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, sender);
Toast.makeText(this, "tii;;; inside", Toast.LENGTH_LONG).show();
}
and the following Broadcastreceiver:
public class AlarmReceiver extends BroadcastReceiver {
private NotificationManager nm;
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Toast.makeText(context, "inside", Toast.LENGTH_LONG).show();
nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from = "Raja";
CharSequence message = "My First App...";
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(), 0);
Notification notif = new Notification(R.drawable.ic_launcher,"Crazy About Android...", System.currentTimeMillis());
notif.setLatestEventInfo(context, from, message, contentIntent);
nm.notify(1, notif);
}
I think your alarm is not getting executed because it has already passed as you are passing day of month as 25
Upvotes: 2