Android Widget Click and Broadcast Receiver Not Working -
the below code should describe app once widget button clicked, sends off intent should received testreceiver. however, in running below code, onreceive of testreceiver never called.
could let me know i'm doing wrong?
widget code
public class widget extends appwidgetprovider { public void onupdate(context context, appwidgetmanager appwidgetmanager, int[] appwidgetids) { final int n = appwidgetids.length; // perform loop procedure each app widget belongs provider (int i=0; i<n; i++) { int appwidgetid = appwidgetids[i]; // create intent launch exampleactivity //intent intent = new intent(context.getapplicationcontext(), testreceiver.class); intent intent = new intent(); intent.setaction(testreceiver.test_intent); intent.setclassname(testreceiver.class.getpackage().getname(), testreceiver.class.getname()); pendingintent pendingintent = pendingintent.getbroadcast(context.getapplicationcontext(), 0, intent, pendingintent.flag_cancel_current); // layout app widget , attach on-click listener button remoteviews views; views = new remoteviews(context.getpackagename(), r.layout.main); views.setonclickpendingintent(r.id.btntest, pendingintent); // tell appwidgetmanager perform update on current app widget appwidgetmanager.updateappwidget(appwidgetid, views); } }
}
receiver code:
public class testreceiver extends broadcastreceiver { public static final string test_intent= "mytestintent"; @override public void onreceive(context context, intent intent) { // todo auto-generated method stub toast.maketext(context, "test", toast.length_short); if(intent.getaction()==test_intent) { system.out.println("got intent"); toast.maketext(context, "test", toast.length_short); } } }
manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.intenttest" android:versioncode="1" android:versionname="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name=".testreceiver" android:label="@string/app_name"> <intent-filter> <action android:name="mytestintent"> </action> </intent-filter> </receiver> <receiver android:label="@string/app_name" android:name="widget"> <intent-filter> <action android:name="android.appwidget.action.appwidget_update" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/widget" /> </receiver> </application> <uses-sdk android:minsdkversion="3" /> </manifest>
it works, forgot add .show()
@ end of toast :)
Comments
Post a Comment