作业问题,无法收到广播
public class MainActivity extends AppCompatActivity {
//private MyBroadcastReceiver myBroadcastReceiver;
private List<AppInfo> appInfoList,resultList;
private List<Bean> beanList;
private Myadapter myadapter;
private SearchView searchView;
private ListView listView;
private static final String TAG = "MainActivity-Tag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerBatteryInfo();
initView();
Log.e(TAG, "33333333333");
}
private void initView() {
myadapter=new Myadapter(this);
myadapter.setAppInfoList(appInfoList);
resultList=new ArrayList<>();
searchView=findViewById(R.id.searchView);
listView=findViewById(R.id.show_lv);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
if (!TextUtils.isEmpty(s)){
resultList.clear();
for (int i=0;i<appInfoList.size();i++){
if ((appInfoList.get(i).getName()).contains(s)){
resultList.add(appInfoList.get(i));
}
}
myadapter.setAppInfoList(resultList);
myadapter.notifyDataSetChanged();
}
else{
myadapter.setAppInfoList(appInfoList);
myadapter.notifyDataSetChanged();
}
return false;
}
});
listView.setAdapter(myadapter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}
private void registerBatteryInfo(){
//myBroadcastReceiver=new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.sample.custom.action");
registerReceiver(broadcastReceiver,intentFilter);
}
public Bitmap getBitmap(byte[] data){
return BitmapFactory.decodeByteArray(data, 0, data.length);//从字节数组解码位图
}
private BroadcastReceiver broadcastReceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "22222222222222");
if (intent!=null){
beanList= (List<Bean>) intent.getSerializableExtra("appList");
appInfoList=new ArrayList<>();
for (int i=0;i<beanList.size();i++){
AppInfo appInfo=new AppInfo();
Bitmap bitmap=getBitmap(beanList.get(i).getPic());
appInfo.setIcon(bitmap);
appInfo.setName(beanList.get(i).getName());
appInfoList.add(appInfo);
}
Log.e(TAG, appInfoList.get(1).getName()+"111111111111111111");
}
}
};
}===============================================================
public class MyService extends Service {
private final String MY_ACTION="com.sample.custom.action";
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
new Thread(){
@Override
public void run() {
super.run();
List<Bean> beanList=getBeans();
Intent intent=new Intent();
intent.setAction(MY_ACTION);
intent.putExtra("appList", (Serializable) beanList);
MyService.this.sendBroadcast(intent);
Log.e("MainActivity-Tag", "service....thread");
}
}.start();
}
public List<Bean> getBeans(){
List<Bean> beans=new ArrayList<>();
PackageManager packageManager = getPackageManager();
List<ApplicationInfo> applicationInfos = packageManager.getInstalledApplications(0);
for (int i=0;i<applicationInfos.size();i++){
//AppInfo appInfo=new AppInfo();
Bean bean=new Bean();
bean.setName(applicationInfos.get(i).loadLabel(packageManager).toString());
BitmapDrawable bitmapDrawable = (BitmapDrawable) applicationInfos.get(i).loadIcon(packageManager);
Bitmap bitmap=bitmapDrawable.getBitmap();
bean.setPic(getBytes(bitmap));
beans.add(bean);
}
return beans;
}
public static byte[] getBytes(Bitmap bitmap){
//实例化字节数组输出流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, baos);//压缩位图
return baos.toByteArray();//创建分配字节数组
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 147 份
- 解答问题 687 个
本专题介绍了Android开发核心组件:广播、服务及全局应用。教会你如何使用AIDL、Thread、Socket、蓝牙解决进程线程间通信问题。利用Glide等实现异步加载及NDK原生代码开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星