我想为我的Android应用程序实现通知,但我不知道哪个类最适合该任务。
我刚看过AlarmManager
,这类电池是否友好?
如果是,有人可以举例说明如何为未来事件设置通知(例如,我设置后2或3天)吗?
我有这个源代码作为示例,它应该在30秒后通过警报和Toast消息通知我,但是不起作用。
package com.rNetworks.testAlarm;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class AndroidTestAlarmActivity extends Activity {
private PendingIntent mAlarmSender;
private OnClickListener lstStart= new OnClickListener(){
@Override
public void onClick(View arg0) {
//
// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
firstTime, 30*1000, mAlarmSender);
// Tell the user about what we did.
Toast.makeText(AndroidTestAlarmActivity.this, "sdasdads",
Toast.LENGTH_LONG).show();
}
};
private OnClickListener lstStop= new OnClickListener(){
@Override
public void onClick(View v) {
AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(mAlarmSender);
Toast.makeText(AndroidTestAlarmActivity.this, "Cancelando Alarma :(",Toast.LENGTH_SHORT).show();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnStart=(Button)findViewById(R.id.btnStart);
Button btnStop=(Button)findViewById(R.id.btnStop);
mAlarmSender = PendingIntent.getService(AndroidTestAlarmActivity.this,
0, new Intent(AndroidTestAlarmActivity.this, Alarm.class), 0);
btnStart.setOnClickListener(lstStart);
btnStop.setOnClickListener(lstStop);
}
}
我想要一些带通知的待办事项列表:)
谢谢:)