吐司没有显示

时间:2012-02-02 21:10:53

标签: android android-activity toast

我正在使用名为MyTimeReceiver的广播接收器每隔一小时(每10秒进行一次测试)显示一个吐司。我的问题是吐司没有显示。

以下是我从主要活动文件(SafeDrive3Activity)剪下的代码:

Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
     //add 10 seconds to calendar object
        cal.add(Calendar.SECOND, 10);
        mAlarmSender = PendingIntent.getBroadcast(SafeDrive3Activity.this,
        0, new Intent(SafeDrive3Activity.this, MyTimeReceiver.class), 0);

      // Schedule the alarm!
        AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, 
        SystemClock.elapsedRealtime(),mAlarmSender); 

broadcastreceiver class:

public class MyTimeReceiver extends BroadcastReceiver 

{

    @Override

  public void onReceive(Context context, Intent intent) {
    Toast.makeText(context,"HOUNOTIFICATION", Toast.LENGTH_LONG).show();
            }
         }

请帮助这让我疯了几个小时。

2 个答案:

答案 0 :(得分:1)

这个到处都是。您创建了一个永不使用的Calendar,告诉AlarmManager使用RTC_WAKEUP然后使用elapsedRealtime()时间来源:)。

试试这个:

am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), mAlarmSender);

答案 1 :(得分:0)

尝试每隔10秒显示一次吐司。

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000*10, mAlarmSender));
相关问题