Android 从服务向接收方发送多个意向
我正在制作一个简单的应用程序,在一天中的8个不同时间生成通知。一个服务正在使用报警管理器生成报警,我正在使用广播接收器捕捉报警并显示toast/创建通知 问题是我不能创建多个报警。我用于创建多个报警的方法对每个报警都有不同的意图。但是,一旦我在模拟器上启动应用程序,所有消息都会在同一时间同时显示。。 我不知道这是不是正确的方法。。 如果是,请帮我改正 如果没有,请建议一些替代方法 我正在附上我的服务和接收器代码Android 从服务向接收方发送多个意向,android,android-intent,Android,Android Intent,我正在制作一个简单的应用程序,在一天中的8个不同时间生成通知。一个服务正在使用报警管理器生成报警,我正在使用广播接收器捕捉报警并显示toast/创建通知 问题是我不能创建多个报警。我用于创建多个报警的方法对每个报警都有不同的意图。但是,一旦我在模拟器上启动应用程序,所有消息都会在同一时间同时显示。。 我不知道这是不是正确的方法。。 如果是,请帮我改正 如果没有,请建议一些替代方法 我正在附上我的服务和接收器代码 package com.example.firstapp; import java
package com.example.firstapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String tag = null;
int number = 0;
// MediaPlayer player;
String str, bread;
StringBuffer buf1, buf2, buf3, buf4, buf5, buf6, buf7, buf8;
Calendar time1, time2, time3, time4, time5, time6, time7, time8;
Bundle basket;;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show();
Log.i(tag, "service created");
// player=MediaPlayer.create(this, R.raw.song);
// player.setLooping(false);
try {
createalarm();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void createalarm() throws IOException {
// TODO Auto-generated method stub
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i1 = new Intent(this, MyAlarmReceiver.class);
Intent i2 = new Intent(this, MyAlarmReceiver.class);
Intent i3 = new Intent(this, MyAlarmReceiver.class);
Intent i4 = new Intent(this, MyAlarmReceiver.class);
Intent i5 = new Intent(this, MyAlarmReceiver.class);
Intent i6 = new Intent(this, MyAlarmReceiver.class);
Intent i7 = new Intent(this, MyAlarmReceiver.class);
Intent i8 = new Intent(this, MyAlarmReceiver.class);
fillbuffers();
/*
* fillbread(4); Bundle basket = new Bundle(); basket.putString("key",
* bread); i.putExtras(basket);
*/
PendingIntent pendingi1,pendingi2,pendingi3,pendingi4,pendingi5,pendingi6,pendingi7,pendingi8;
time1 = Calendar.getInstance();
time2 = Calendar.getInstance();
time3 = Calendar.getInstance();
time4 = Calendar.getInstance();
time5 = Calendar.getInstance();
time6 = Calendar.getInstance();
time7 = Calendar.getInstance();
time8 = Calendar.getInstance();
time1.setTimeInMillis(System.currentTimeMillis());
time1.set(Calendar.HOUR_OF_DAY, 16);
time1.set(Calendar.MINUTE, 40);
time1.set(Calendar.SECOND, 0);
time1.set(Calendar.MILLISECOND, 0);
time2.setTimeInMillis(System.currentTimeMillis());
time2.set(Calendar.HOUR_OF_DAY, 16);
time2.set(Calendar.MINUTE, 41);
time2.set(Calendar.SECOND, 0);
time2.set(Calendar.MILLISECOND, 0);
time3.setTimeInMillis(System.currentTimeMillis());
time3.set(Calendar.HOUR_OF_DAY, 16);
time3.set(Calendar.MINUTE, 42);
time3.set(Calendar.SECOND, 0);
time3.set(Calendar.MILLISECOND, 0);
time4.setTimeInMillis(System.currentTimeMillis());
time4.set(Calendar.HOUR_OF_DAY, 16);
time4.set(Calendar.MINUTE, 43);
time4.set(Calendar.SECOND, 0);
time4.set(Calendar.MILLISECOND, 0);
time5.setTimeInMillis(System.currentTimeMillis());
time5.set(Calendar.HOUR_OF_DAY, 16);
time5.set(Calendar.MINUTE, 44);
time5.set(Calendar.SECOND, 0);
time5.set(Calendar.MILLISECOND, 0);
time6.setTimeInMillis(System.currentTimeMillis());
time6.set(Calendar.HOUR_OF_DAY, 16);
time6.set(Calendar.MINUTE,45);
time6.set(Calendar.SECOND, 0);
time6.set(Calendar.MILLISECOND, 0);
time7.setTimeInMillis(System.currentTimeMillis());
time7.set(Calendar.HOUR_OF_DAY, 16);
time7.set(Calendar.MINUTE, 46);
time7.set(Calendar.SECOND, 0);
time7.set(Calendar.MILLISECOND, 0);
time8.setTimeInMillis(System.currentTimeMillis());
time8.set(Calendar.HOUR_OF_DAY, 16);
time8.set(Calendar.MINUTE, 47);
time8.set(Calendar.SECOND, 0);
time8.set(Calendar.MILLISECOND, 0);
// fillbread();
//if (true) {
fillbread(1);
basket = new Bundle();
basket.putString("key", bread);
i1.putExtras(basket);
pendingi1 = PendingIntent.getBroadcast(this, 0, i1,PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time1.getTimeInMillis(),
pendingi1);
//}
//if (System.currentTimeMillis() == time2.getTimeInMillis()) {
fillbread(2);
basket = new Bundle();
basket.putString("key", bread);
i2.putExtras(basket);
pendingi2 = PendingIntent.getBroadcast(this, 0, i2,PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time2.getTimeInMillis(),
pendingi2);
// }
// if (System.currentTimeMillis() == time3.getTimeInMillis()) {
fillbread(3);
basket = new Bundle();
basket.putString("key", bread);
i3.putExtras(basket);
pendingi3 = PendingIntent.getBroadcast(this, 0, i3,PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, time3.getTimeInMillis(),
pendingi3);
// }
//if (System.currentTimeMillis() == time4.getTimeInMillis()) {
fillbread(4);
basket = new Bundle();
basket.putString("key", bread);
i4.putExtras(basket);
pendingi4 = PendingIntent.getBroadcast(this, 0, i4,PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
pendingi4);
// }
//else if (System.currentTimeMillis() == time5.getTimeInMillis()) {
fillbread(5);
basket = new Bundle();
basket.putString("key", bread);
i5.putExtras(basket);
pendingi5 = PendingIntent.getBroadcast(this, 0, i5,PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
pendingi5);
// } else if (System.currentTimeMillis() == time6.getTimeInMillis()) {
fillbread(6);
basket = new Bundle();
basket.putString("key", bread);
i6.putExtras(basket);
pendingi6 = PendingIntent.getBroadcast(this, 0, i6,PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
pendingi6);
// } else if (System.currentTimeMillis() == time7.getTimeInMillis()) {
fillbread(7);
basket = new Bundle();
basket.putString("key", bread);
i7.putExtras(basket);
pendingi7 = PendingIntent.getBroadcast(this, 0, i7,PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
pendingi7);
//} else if (System.currentTimeMillis() == time8.getTimeInMillis()) {
fillbread(8);
basket = new Bundle();
basket.putString("key", bread);
i8.putExtras(basket);
pendingi8 = PendingIntent.getBroadcast(this, 0, i8,PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
pendingi8);
// }
}
private void fillbread(int number) {
// TODO Auto-generated method stub
switch (number) {
case 1:
bread = buf1.toString();
break;
case 2:
bread = buf2.toString();
break;
case 3:
bread = buf3.toString();
break;
case 4:
bread = buf4.toString();
break;
case 5:
bread = buf5.toString();
break;
case 6:
bread = buf6.toString();
break;
case 7:
bread = buf7.toString();
break;
case 8:
bread = buf8.toString();
break;
}
}
private void fillbuffers() throws IOException {
// TODO Auto-generated method stub
StringBuffer buf = new StringBuffer();
buf1 = new StringBuffer();
buf2 = new StringBuffer();
buf3 = new StringBuffer();
buf4 = new StringBuffer();
buf5 = new StringBuffer();
buf6 = new StringBuffer();
buf7 = new StringBuffer();
buf8 = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.drawable.yo);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
int index = 1, tcount = 0;
String line;
if (is != null) {
while ((line = reader.readLine()) != null && tcount < 8) {
switch (index) {
case 1:
buf1.append(line + "\n");
break;
case 2:
buf2.append(line + "\n");
break;
case 3:
buf3.append(line + "\n");
break;
case 4:
buf4.append(line + "\n");
break;
case 5:
buf5.append(line + "\n");
break;
case 6:
buf6.append(line + "\n");
break;
case 7:
buf7.append(line + "\n");
break;
case 8:
buf8.append(line + "\n");
break;
}
buf.append(line + "\n");
tcount = tcount + 1;
index = index + 1;
}
}
// bread=buf1.toString();
is.close();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "service destoryed", Toast.LENGTH_LONG).show();
// player.stop();
}
@SuppressWarnings("deprecation")
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.i(tag, "service started");
// player.start();
}
我猜这是因为您使用了FLAG\u UPDATE\u CURRENT FLAG
pendingi1 = PendingIntent.getBroadcast(this, 0, i1,PendingIntent.FLAG_UPDATE_CURRENT);
如果您不想更换以前的悬挂式帐篷,请在任何地方使用FLAG_ONE_SHOT
pendingi1 = PendingIntent.getBroadcast(this, 0, i1,PendingIntent.FLAG_ONE_SHOT);
我认为发生这种情况是因为您对所有重叠的对象使用相同的通知id 因此,对所有nm.notify(0,notify)使用不同的ID; 将0替换为1,2,3
更多访问不,当我从另一个活动启动服务时,所有的祝酒词都会立即出现。我看到你将时间硬编码为下午4:40,设备上的时间是否超过这个时间?实际上每次我运行应用程序,我确保所有时间值都在仿真器时间之前只是为了检查而不是设置时间,请尝试类似time2.setTimeInMillis(System.currentTimeMillis()+60000)的操作。不..它不工作…现在,只显示第一条消息。。如果我将更改为:time1.setTimeInMillis=System.currentTimeMillis()+60000 time2.setTimeInMillis=System.currentTimeMillis()+120000…如下..仅显示第一条消息..之后..什么也没有发生
pendingi1 = PendingIntent.getBroadcast(this, 0, i1,PendingIntent.FLAG_ONE_SHOT);