Package com. JK. broadcastreceivertest;
Import java. util. hashmap;
Import java. util. iterator;
Import java. util. List;
Import java. util. Map;
Import java. util. Map. entry;
Import java. util. Set;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. App. activity;
Import Android. App. icationicationmanager;
Import Android. content. context;
Import Android. content. intent;
Import Android. content. intentfilter;
Import Android. content. PM. applicationinfo;
Import Android. content. PM. packagemanager;
Import Android. content. PM. resolveinfo;
Import Android. util. log;
Import Android. View. Menu;
Import Android. View. view;
Import Android. widget. Button;
Import Android. widget. edittext;
Public class mainactivity extends activity {
Private Static final string tag = "mainactivity ";
Private Static final int msg_send = 1;
Button btbc;
Button btre;
Button btunre;
Button btunre2;
Private Static final string action = "JK. Action ";
Private Static final string Action2 = "JK. Call ";
Mycycler aggreger;
Boolean isconn = false;
Handler mhandler = new handler (){
Public void handlemessage (Message MSG ){
Switch (msg. What ){
Case msg_send:
Btunre. settext ("canceling registration ...");
Btunre. settext (msg. obj. tostring ());
Break;
}
}
};
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. activity_main );
Btbc = (button) findviewbyid (R. Id. btbc );
Btre = (button) findviewbyid (R. Id. btre );
Btunre = (button) findviewbyid (R. Id. btunre );
Btunre2 = (button) findviewbyid (R. Id. btunre2 );
Btre. setenabled (false );
Btunre. setenabled (false );
// Dynamic Registration
Btre. setonclicklistener (New View. onclicklistener (){
@ Override
Public void onclick (view v ){
Extends ER = new myreceiver ();
Intentfilter intentfliter = new intentfilter (action );
Intentfliter. addaction (action );
Registerreceiver (receiver, intentfliter );
Intent I = new intent (action );
Sendbroadcast (I );
Log. I (TAG, "register ");
Btre. settext ("registered ...");
Btunre. settext ("cancel registration ");
Isconn = true;
}
});
Btunre. setonclicklistener (New View. onclicklistener (){
@ Override
Public void onclick (view v ){
Log. I (TAG, "Cycler" + cycler. getresultcode ());
Log. I (TAG, "Cycler" + cycler. getresultdata ());
/* If (Explorer. getresultdata ()! = NULL )*/
If (isconn ){
Unregisterreceiver (receiver );
Log. I (TAG, "cancel registration ");
Btre. settext ("register ");
New thread (){
@ Override
Public void run (){
Super. Run ();
Message MSG = new message ();
MSG. What = msg_send;
MSG. OBJ = "finished! ";
Mhandler. sendmessage (MSG );
}
}. Start ();
Isconn = false;
}
}
});
// Static registration
Btbc. setonclicklistener (New View. onclicklistener (){
@ Override
Public void onclick (view v ){
Intent intent = new intent ();
Intent. setaction (Action2 );
Sendbroadcast (intent );
Log. I (TAG, "broadcast ");
Btbc. settext ("sending ...");
// Testpackage ();
Testreceiver ();
}
});
Btunre2.setonclicklistener (New View. onclicklistener (){
/* A broadcastreceiver object is only valid for the duration of the call to onreceive (context, intent ).
Once your code returns from this function, the system considers the object to be finished and no longer active .*/
@ Override
Public void onclick (view v ){
Finish ();
Btbc. settext ("broadcast ");
// Testpackage ();
Testreceiver ();
}});
}
// This segment is obtained from other places on csdn
Private hashmap <string, string> installpackagesinfo (){
// Obtain the packagemanager object
Packagemanager = This. getpackagemanager ();
/* Getinstalledapplications return the set of application packages installed on the Current Device
* Applicationinfo corresponds to the application tag in androidmanifest. xml. It can be used to obtain information about the application.
*/
List <applicationinfo> applicationinfos = packagemanager. getinstalledapplications (0 );
Hashmap <string, string> resultmap = new hashmap <string, string> ();
Iterator <applicationinfo> iterator = applicationinfos. iterator ();
While (iterator. hasnext ()){
Applicationinfo = iterator. Next ();
String packagename = applicationinfo. packagename; // package name
String packagelabel = packagemanager. getapplicationlabel (applicationinfo). tostring (); // obtain the label
Resultmap. Put (packagelabel, packagename );
}
Return resultmap;
}
Void testpackage (){
Hashmap <string, string> hashmap = installpackagesinfo ();
Log. I (TAG, "size" + hashmap. Size ());
Set <entry <string, string> set = hashmap. entryset ();
Iterator <entry <string, string> itrator = set. iterator ();
While (itrator. hasnext ()){
Entry <string, string> E = itrator. Next ();
If (E. getkey (). Equals ("broadcastreceivertest ")){
Log. I (TAG, "E" + E. getvalue ());
}
}
}
Void testreceiver (){
Packagemanager = This. getpackagemanager ();
Intent intent = new intent (Action2 );
List <resolveinfo> List = packagemanager. querybroadcastreceivers (intent, 0 );
Iterator <resolveinfo> itrator = List. iterator ();
While (itrator. hasnext ()){
Resolveinfo rI = (resolveinfo) itrator. Next ();
Log. I (TAG, "Ri" + list. Size () + ":" + Ri. resolvepackagename );
}
// Icationicationmanager Nm = (notificationmanager) getsystemservice (context. activity_service );
}
}
Package com. JK. broadcastreceivertest;
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. content. intentfilter;
Import Android. util. log;
Import Android. widget. edittext;
Public class broadcast extends broadcastreceiver {
Private Static final string tag = "mainactivity ";
Public Broadcast (){
Super ();
Log. I (TAG, "oncreate ");
}
@ Override
Public void onreceive (context, intent ){
Log. I (TAG, "broadcast onreceive ");
// String data = intent. getdatastring ();
// Log. I (TAG, "data" + data );
}
}
Package com. JK. broadcastreceivertest;
Import Android. App. activity;
Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. content. intentfilter;
Import Android. util. log;
Import Android. widget. edittext;
Import Android. widget. Toast;
Public class myreceiver extends broadcastreceiver {
Private Static final string tag = "mainactivity ";
Public myreceiver (){
Super ();
Log. I (TAG, "oncreate ");
}
@ Override
Public void onreceive (context, intent ){
Log. I (TAG, "myreceiver onreceive ");
String STR = intent. getaction ();
Toast. maketext (context, STR, Toast. length_short). Show ();
}
}
<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. JK. broadcastreceivertest"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Uses-SDK
Android: minsdkversion = "8"
Android: targetsdkversion = "16"/>
<Application
Android: allowbackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: Label = "@ string/app_name"
Android: theme = "@ style/apptheme">
<Activity
Android: Name = "com. JK. broadcastreceivertest. mainactivity"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
<Cycler
Android: Name = ". Broadcast"
Android: exported = "false">
<Intent-filter>
<Action Android: Name = "JK. Call"/>
</Intent-filter>
</Cycler>
<! -- <Cycler
Android: Name = ". myreceiver"
Android: exported = "false">
<Intent-filter>
<Action Android: Name = "JK. Action"/>
</Intent-filter>
</Explorer> -->
</Application>
</Manifest>