android - BroadcastReceiver passing value error -
i want pass integer
value activity
class(playlistactivity.java
) service
(audioservice.java
) class. when click on listitem
, application gets stopped. couldn`t figure error. hope me
error in logcat:
java.lang.runtimeexception: error recieveing broadcast intent {act-com.exaple.serviceaudio.audioservice.play_song flg=0x10 (has extras)} in com.example.serviceaudio.audioservice$audioplayerbroadcastreciever@41b4e818.........
here codes.
playllistactivity.java
lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { playsongsong(position); }}); @override protected void ondestroy() { // todo auto-generated method stub unbindservice(serviceconnection); super.ondestroy(); } public void playsongsong(int songindex){ intent intent = new intent(audioservice.play_song); intent.putextra("songindex", songindex); this.sendbroadcast(intent); }
audioservice.java
public static final string play_song_base = "com.example.serviceaudio.audioservice"; public static final string play_song = play_song_base + ".play_song"; private audioplayerbroadcastreceiver broadcastreceiver = new audioplayerbroadcastreceiver(); @override public void oncreate() { log.v("audioservice", "audioplayer: oncreate() called"); intentfilter intentfilter = new intentfilter(); intentfilter.addaction(play_song); registerreceiver(broadcastreceiver, intentfilter); } public void ondestroy() { unregisterreceiver(broadcastreceiver); } private class audioplayerbroadcastreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { string action = intent.getaction(); int currentsongindex = intent.getextras().getint("songindex"); if(play_song.equals(action)) { log.d("audioservice", "action not recognized: " + action); playsong(currentsongindex); }}
when comment int currentsongindex = intent.getextras().getint("songindex");
, `playsong(currentsongindex); there no error doing nothing.
a broadcastreceiver object valid duration of call onreceive(context, intent). once code returns function, system considers object finished , no longer active.
this has important repercussions can in onreceive(context, intent) implementation: requires asynchronous operation not available, because need return function handle asynchronous operation, @ point broadcastreceiver no longer active , system free kill process before asynchronous operation completes.
in short life of broadcast receiver short , shot not long running task in it. @ max try raise intent , work there.
this causing crash , when comment works fine you.
Comments
Post a Comment