嗨想在我的应用程序中播放警报,所以当我单击开始按钮警报正在播放时,我尝试使用下面的代码使用服务。但我想要的是,相同的活动我首先有两个编辑文本,我有时间 9,第二个编辑文本我已经给了时间 10,所以我必须只在晚上 9 点和晚上 10 点显示通知……任何人都给我一些想法。
public class DonnsAlarm extends Activity {
/** Called when the activity is first created. */
EditText e1,e2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
e1=(EditText)findViewById(R.id.editText1);
e2=(EditText)findViewById(R.id.editText2);
// click listener for the button to start service
Button btnStart = (Button) findViewById(R.id.button1);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s1=e1.getText().toString();
String s2=e2.getText().toString();
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(s1));
cal.add(Calendar.SECOND, 10);
Intent intent = new Intent(DonnsAlarm.this, Service_class.class);
PendingIntent pintent = PendingIntent.getService(DonnsAlarm.this, 0, intent,
0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
36000 * 1000, pintent);
PendingIntent pintent1 = PendingIntent.getService(DonnsAlarm.this, 1, intent,
0);
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(s2));
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
36000 * 1000, pintent1);
}
});
// click listener for the button to stop service
Button btnStop = (Button) findViewById(R.id.button2);
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(getBaseContext(), Service_class.class));
}
});
}
}
public class Service_class extends Service {
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
Intent notifyIntent = new Intent(Intent.ACTION_MAIN);
notifyIntent.setClass(getApplicationContext(), DonnsAlarm.class);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence from ="It's Time to Play VocCards!";
CharSequence message = "It's Time to Play VocCards!";
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
Notification notif = new Notification(R.drawable.ic_launcher,
"It's Time to Play VocCards!", System.currentTimeMillis());
notif.setLatestEventInfo(this, from, message, contentIntent);
nm.notify(1, notif);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}
@Override
public void onCreate() {
super.onCreate();
}
}