2016-11-14 11 views
1

私が作成したParcelable Java Objectのフィールドを表示するには、ある時間に警告を出そうとしています。私は以前これに関連する質問があることを知っていますが、私はこの問題の解決策を見いだせませんでした。 Receiverは、データが渡されないと正常にアラートを作成しますが、を作成するFragmentIntentから送信するデータは保持されません。データがbroadcastReceiverに送信されていません

Receiverのための私のコードは以下の通りです:

public class NotificationReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    IUser eu = intent.getExtras().getParcelable("USER"); 
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent repeating_intent = new Intent(new Intent(context, MainActivity.class)); 
    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context) 
      .setContentIntent(pendingIntent) 
      .setSmallIcon(android.R.drawable.arrow_up_float) 
      .setContentTitle("Warning: Approaching Threshold") 
      .setContentText(eu.get_forecast_info().get_estimate_cur_cycle_total_cost()+" is the price") 
      .setAutoCancel(true); 

    notificationManager.notify(100, builder.build()); 
    } 
} 

ReceiverためIntentを供給するためのコードは以下の通りです:アンドロイドの最新バージョンで

trigger_b = (Button) view.findViewById(R.id.trigger_button); 
    trigger_b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      Calendar calendar = Calendar.getInstance(); 
      calendar.set(Calendar.HOUR_OF_DAY, 10); 
      calendar.set(Calendar.MINUTE, 14); 
      calendar.set(Calendar.SECOND, 0); 
      Intent intent = new Intent(getActivity().getApplicationContext(), NotificationReceiver.class); 
      Bundle c = new Bundle(); 
      c.putParcelable("USER", eu); 
      intent.putExtras(c); 
      PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
      AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE); 
      alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 
     } 
    }); 
+0

あなたがテストしているAndroidのバージョンは何ですか? –

+0

私はバージョン25を使用しています! – James

答えて

1

、あなたは確実にカスタムを置くことができないParcelableまたはIntentSerializableオブジェクトで、AlarmManagerに渡されます。カスタムオブジェクトをbyte[]にシリアライズし、byte[]Intentに挿入する必要があります。 onReceive()では、byte[]から再度オブジェクトを構築する必要があります。

コード例については、How to marshall and unmarshall a Parcelable to a byte array with help of Parcel?またはhttps://gist.github.com/jacklt/6711967を参照してください。

+0

これは100%正しいです!ありがとうございました! – James

関連する問題