2012-12-05 46 views
1

我正在做一個簡單的應用程序,在一天中的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(); 
} 

}

Reciever代碼::

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(); 
    */ 
} 
} 
+0

是任何人那裏? – Yashasvi

回答

0

我想這種情況正在發生,因爲你正在使用FLAG_UPDATE_CURRENT標誌

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

使用FLAG_ONE_SHOT無處不在,如果你不想被取代了以往的PendingIntent。

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

不,它不工作 只要我從另一個活動開始服務,所有的敬酒立刻出現。 – Yashasvi

+0

我看到你的硬編碼時間到了下午4:40,設備上的時間還不止於此? – nandeesh

+0

實際上,每當我運行應用程序,我確保所有的時間值都超過了模擬器時間 – Yashasvi

相關問題