开发者

Android AlarmManager

Ok, I've tried two examples of AlarmManager- one from the commonsware website, and one from the manning website. The code I am currently working with is from the manning website : [http://unlocking-android.googlecode.com/svn/chapter8/trunk/SimpleAlarm/][1]

There are two classes, AlarmReceiver and GenerateAlarm. Anyone have any idea why the toast will not display in the emulator? I was thinking that it was because I am located in the Eastern Time Zone and it uses UTC, but I have fiddled with different things and none of them seem to work.

public class GenerateAlarm extends Activity {

Toast mToast;

@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    Button button = (Button) findViewById(R.id.set_alarm_button);
    button.setOnClickListener(this.mOneShotListener);
}

private OnClickListener mOneShotListener = new OnClickListener() {

    public void onClick(View v) {

        Intent intent = new Intent(GenerateAlarm.this, AlarmReceiver.class);

        PendingIntent appIntent = PendingIntent.getBroadcast(GenerateAlarm.this, 0, intent, 0);

        Calendar 开发者_运维知识库calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
       // calendar.add(Calendar.MINUTE, 1);
        calendar.add(Calendar.SECOND, 10);

        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), appIntent);

        if (GenerateAlarm.this.mToast != null) {
            GenerateAlarm.this.mToast.cancel();
        }
        GenerateAlarm.this.mToast = Toast.makeText(GenerateAlarm.this, R.string.alarm_message, Toast.LENGTH_LONG);
        //GenerateAlarm.this.mToast.show();
    }
};

}

public class AlarmReceiver extends BroadcastReceiver {

public void onReceiveIntent(Context context, Intent intent) {
    Toast.makeText(context, R.string.app_name, Toast.LENGTH_SHORT).show();
}

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

}
}


You have to add your toast in the onReceived method where you should add the handling of the intent received.

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, R.string.app_name, Toast.LENGTH_SHORT).show();
}

onReceiveIntent is not a method of broadcastreceiver

public abstract void onReceive (Context context, Intent intent)

Since: API Level 1 This method is called when the BroadcastReceiver is receiving an Intent broadcast.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜