java - Passing value between 3 activities -
in app alarm creates notification works trying pass string value main class fooditems.java alarmreceiver.java in turn passes string ringtoneserviceprovider.java creates custom notification displayed. stands string appears passing alarmreceiver.java had toast message display before see when goes ringtoneserviceprovider.java becomes "null".
this part of fooditems.java string value 'name' value entered user want appear on notification.
private void setalarm(calendar targetcal){ toast.maketext(getapplicationcontext(), "alarm set " + targetcal.gettime(),toast.length_long).show(); intent intent = new intent(getbasecontext(), alarmreceiver.class); intent.putextra("data",name); intent.putextra("id", alarmnum); pendingintent pendingintent = pendingintent.getbroadcast(getbasecontext(),alarmnum,intent, 0); alarmmanager alarmmanager = (alarmmanager)getsystemservice(context.alarm_service); alarmmanager.set(alarmmanager.rtc_wakeup, targetcal.gettimeinmillis(), pendingintent); }
this alarmreceiver.java
package com.example.kev00_000.kitchenhero; import android.app.notificationmanager; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; public class alarmreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { string name = intent.getstringextra("data"); int id = intent.getintextra("id", 1); intent service_intent=new intent(context, ringtoneplayingservice.class); service_intent.putextra("data",name); service_intent.putextra("id", id); context.startservice(service_intent); notificationmanager notifications = (notificationmanager) context.getsystemservice(context.notification_service); } }
and here ringtoneplayingservice.java
import android.os.ibinder; import android.support.annotation.nullable; import android.support.v4.app.notificationcompat; public class ringtoneplayingservice extends service { mediaplayer alarm; private string name; private static int notification_id = 1; @nullable @override public ibinder onbind(intent intent) { name = intent.getstringextra("data"); notification_id = intent.getintextra("id", 1); return null; } public void oncreate(){ super.oncreate(); alarm=mediaplayer.create(this, r.raw.alarmclockbuzz); alarm.setlooping(true); intent stopself = new intent(this, stopalarm.class); pendingintent pendingintent = pendingintent.getbroadcast(this, 0, stopself, pendingintent.flag_cancel_current); final notificationcompat.builder notification = new notificationcompat.builder(getapplicationcontext()) .setsmallicon(r.drawable.ic_launcher) .setongoing(true) .setcontenttitle("kitchenhero") .setcontenttext("time put "+name+" on!!") .setautocancel(true) .setpriority(notificationcompat.priority_high) .setvibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }) .addaction(r.drawable.ic_launcher, "stop", pendingintent); notification note=notification.build(); startforeground(notification_id, note); } public int onstartcommand(intent intent, int flags, int startid) { alarm.start(); return start_not_sticky; } public void ondestroy() { alarm.stop(); alarm.release(); }
using shared preferences this
public static final string mypreferences = "myprefs" ; sharedpreferences sharedpreferences; private void setalarm(calendar targetcal){ sharedpreferences = getsharedpreferences(mypreferences, context.mode_private); sharedpreferences.editor editor = sharedpreferences.edit(); editor.putstring("name",name); editor.putstring("id",id); editor.commit(); toast.maketext(getapplicationcontext(), "alarm set " + targetcal.gettime(),toast.length_long).show(); intent intent = new intent(getbasecontext(), alarmreceiver.class); pendingintent pendingintent = pendingintent.getbroadcast(getbasecontext(),alarmnum,intent, 0); alarmmanager alarmmanager = (alarmmanager)getsystemservice(context.alarm_service); alarmmanager.set(alarmmanager.rtc_wakeup, targetcal.gettimeinmillis(), pendingintent); }
and string in other class
sharedpreferences preferences = preferencemanager.getdefaultsharedpreferences(this); string name = preferences.getstring("name", "");
Comments
Post a Comment