Android Emulator不适用于Alarm Reminder程序

时间:2012-03-27 16:55:09

标签: android android-emulator alarmmanager

我是Android新手。我已经尝试了状态栏提醒的示例应用程序,以提及日期。但它不适用于模拟器,我不知道我错过了什么。我在这里附上了示例代码

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);

在闹钟接收课程中,我有以下代码

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);
  }
 }

在清单文件中,我为警报接收类

添加了以下行(在应用程序标记关闭之前)
  <receiver android:name=".AlarmReceiver"></receiver>

记录cat条目


    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

1 个答案:

答案 0 :(得分:2)

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(), 0);

你在这里传递一个空白的意图。将此更改为您想要的所有活动的意图,例如:

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context,MyActivity.class), 0);

尝试以下活动:

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();
    }

以及以下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);
    }

我认为你的警报没有被执行,因为它已经过了,因为你已经过了一个月的25日

相关问题