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.NotificationManager;
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";
MyReceiver receiver;
boolean isConn = false;
Handler mHandler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case MSG_SEND:
btunre.setText("取消注册中...");
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);
//动态注册
btre.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
receiver = new MyReceiver();
IntentFilter intentfliter = new IntentFilter(ACTION);
intentfliter.addAction(ACTION);
registerReceiver(receiver, intentfliter);
Intent i = new Intent(ACTION);
sendBroadcast(i);
Log.i(TAG, "注册");
btre.setText("注册中...");
btunre.setText("取消注册");
isConn = true;
}
});
btunre.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG,"receiver " + receiver.getResultCode());
Log.i(TAG,"receiver " + receiver.getResultData());
/*if(receiver.getResultData() != null)*/
if(isConn){
unregisterReceiver(receiver);
Log.i(TAG, "取消注册");
btre.setText("注册");
new Thread(){
@Override
public void run() {
super.run();
Message msg = new Message();
msg.what = MSG_SEND;
msg.obj = "完成!";
mHandler.sendMessage(msg);
}
}.start();
isConn = false;
}
}
});
//静态注册
btbc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(ACTION2);
sendBroadcast(intent);
Log.i(TAG, "广播");
btbc.setText("发送中...");
//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("广播");
//testPackage();
testReceiver();
}});
}
//此段从csdn上其他地方所得
private HashMap<String, String> installPackagesInfo(){
// 获取packageManager对象
PackageManager packageManager = this.getPackageManager();
/*getInstalledApplications 返回当前设备上安装的应用包集合
* ApplicationInfo对应着androidManifest.xml中的application标签。通过它可以获取该application对应的信息
*/
List<ApplicationInfo> applicationInfos = packageManager.getInstalledApplications(0);
HashMap<String, String> resultMap = new HashMap<String, String>();
Iterator<ApplicationInfo> iterator = applicationInfos.iterator();
while(iterator.hasNext()){
ApplicationInfo applicationInfo = iterator.next();
String packageName = applicationInfo.packageName;// 包名
String packageLabel = packageManager.getApplicationLabel(applicationInfo).toString();//获取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 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);
}
//NotificationManager 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 context, Intent 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 context, Intent 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" >
<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>
<receiver
android:name=".Broadcast"
android:exported="false">
<intent-filter>
<action android:name="jk.call"/>
</intent-filter>
</receiver>
<!-- <receiver
android:name=".MyReceiver"
android:exported="false">
<intent-filter>
<action android:name="jk.action"/>
</intent-filter>
</receiver> -->
</application>
</manifest>