android - Broadcast Send/Receive between Activities -
new android , trying implement broadcast communication between 2 activities. i've looked @ many examples in books, web sites, , have followed several threads here, still missing something. samples have activity extends broadcastactivity, believe not needed app. put straight forward sample test.
what missing?? entire code below.
thanks help.
package com.dialogtest.dt; import android.app.activity; import android.app.alertdialog; import android.app.dialog; import android.content.dialoginterface; import android.os.bundle; import android.text.editable; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.widget.edittext; import android.widget.textview; public class dialogactivity extends activity { public static final string dtag = "adebug"; public static string strdia = "declr"; public static string broadmess = "com.dialogtest.dt.restart"; public static textview tv; } import android.app.activity; import android.app.alertdialog; import android.app.dialog; import android.content.broadcastreceiver; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.content.intentfilter; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.widget.button; import android.widget.textview; public class mainactivity extends dialogactivity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); strdia = "one"; tv = (textview) findviewbyid(r.id.textview_main); final button diabutton = (button) findviewbyid(r.id.button_a); diabutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { startactivity(new intent(mainactivity.this, testactivity.class)); } }); } broadcastreceiver receiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { tv.settext("success - received!"); log.e(dtag, "in main - received broadcast - str: " + strdia); } }; @override protected void onresume() { intentfilter filter = new intentfilter(); filter.addaction(broadmess); registerreceiver(receiver, filter); super.onresume(); } @override protected void onpause() { unregisterreceiver(receiver); super.onpause(); } } public class testactivity extends dialogactivity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.two_dialog); final button db = (button) findviewbyid(r.id.buttondone); db.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { finish(); } }); } @override public void onstart() { super.onstart(); edittext det = (edittext) findviewbyid(r.id.edittext01); det.settext(strdia); } @override public void onpause() { super.onpause(); edittext det = (edittext) findviewbyid(r.id.edittext01); strdia = det.gettext().tostring(); intent broadcast = new intent(); broadcast.setaction(broadmess); sendbroadcast(broadcast); log.e(dtag, "in test onpause sending broadcast - strdia: " + strdia); } } <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dialogtest.dt" android:versioncode="1" android:versionname="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:label="@string/app_name" android:name="mainactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="dialogactivity"></activity> <activity android:name="testactivity" android:theme="@android:style/theme.dialog"></activity> </application> <uses-sdk android:minsdkversion="3" /> </manifest>
it not make sense use broadcast communicate between 2 activities. 1 of activities ever active @ time. make sense use communicate between activity , service, example.
i don't know trying communicate, can use intent extras pass information next activity. i.e, using putextra.
(java.lang.string, android.os.bundle)
Comments
Post a Comment