从服务向接收者发送多个意图

时间:2012-12-05 14:46:05

标签: android android-intent

我正在创建一个简单的应用程序,它在一天中的8个不同时间生成通知。服务使用警报管理器生成警报,我使用广播接收器捕获警报并显示吐司/创建通知。

问题在于我无法创建多个警报。我用来创建多个警报的方法对于每个警报都是一个不同的意图..但是一旦我在模拟器上启动应用程序,所有消息一次同时显示.. 我不知道这是不是正确的方法.. 如果有,请帮我纠正 如果没有,请提出一些替代方法

我附加了服务和接收器的代码

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();
}

}

接收码::

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MyAlarmReceiver extends BroadcastReceiver {

@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Bundle gotBasket = intent.getExtras();
    String msg = gotBasket.getString("key");
    Toast.makeText(context, msg, Toast.LENGTH_LONG).show();

    NotificationManager nm =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    @SuppressWarnings("deprecation")
    Notification notify=new Notification(android.R.drawable.stat_notify_missed_call, msg, System.currentTimeMillis());
    Intent intent1= new Intent(context, DisplayMessage.class);
    Bundle basket = new Bundle();
    basket.putString("msg", msg);
    intent1.putExtras(basket);
    PendingIntent pending= PendingIntent.getActivity(context, 0, intent1, 0);
    notify.setLatestEventInfo(context, msg, "just a greeting message for you", pending);

    Notification noti = new Notification.Builder(context)
    .setContentTitle("just a greeting message for you")
    .setContentText(msg)
    .setSmallIcon(android.R.drawable.alert_dark_frame)
    .build();

    nm.notify(0, notify);
    /*
     * Intent ourIntent= new Intent(context, Menu.class);
     * context.startActivity(ourIntent); //startAcTivity(ourIntent);
     */
    /*
     * AlertDialog.Builder builder = new AlertDialog.Builder(context);
     * builder.setMessage(msg); builder.setCancelable(false);
     * builder.setPositiveButton("ok", new DialogInterface.OnClickListener()
     * {
     * 
     * @Override public void onClick(DialogInterface dialog, int which) { //
     * TODO Auto-generated method stub } }); AlertDialog alert=
     * builder.create(); alert.show();
     */
}
}

2 个答案:

答案 0 :(得分:0)

我想这是因为你正在使用FLAG_UPDATE_CURRENT标志

   pendingi1 = PendingIntent.getBroadcast(this, 0, i1,PendingIntent.FLAG_UPDATE_CURRENT);

如果您不希望替换先前的pendingIntent,请在任何地方使用FLAG_ONE_SHOT。

   pendingi1 = PendingIntent.getBroadcast(this, 0, i1,PendingIntent.FLAG_ONE_SHOT);

答案 1 :(得分:0)

我认为这种情况正在发生,因为您对所有重叠的用户使用相同的通知ID  因此对所有使用不同的ID是nm.notify(0,notify); 将0替换为1,2,3 ...

更多访问How to create Multiple statusbar Notifications in android