在android frameworks裏新增廣播接受的注意事項

阅读更多

調用注冊廣播:

 private IntentFilter intentFilter ;
 private receiveBroadcastReceiver mreceiveDialogBroadcastReceiver;


 IntentFilter intentFilter = new IntentFilter();
 intentFilter.addAction("com.receiveotamessage.broadcast");
 mreceiveDialogBroadcastReceiver = new receiveBroadcastReceiver();
 mContext.registerReceiver(mreceiveDialogBroadcastReceiver,intentFilter);

 

另一個Class開啓後將數據通過廣播的方式回傳給上一個Class:

 

Intent closeintent = new Intent();
closeintent.setAction("com.receiveotamessage.broadcast");	
if(isNeedUpdate) {
         readUpdateZipFile(updateFilePath);
         closeintent.putExtra("checkresult", "true");	
         closeintent.putExtra("updatepath",updateFilePath);	
         closeintent.putExtra("offset", offset);	
         closeintent.putExtra("size", size);
         closeintent.putExtra("header", headerKeyValuePairs);
         if(maxExternalTimesTamp>maxInternalTimesTamp) {
               closeintent.putExtra("imagetimestamp", String.valueOf(maxExternalTimesTamp));
          }else{	
               closeintent.putExtra("imagetimestamp", String.valueOf(maxInternalTimesTamp))
;	
          }	
}else{
          closeintent.putExtra("checkresult", "false");
}
closeintent.putExtra("currentversion",currentVersion);
closeintent.putExtra("systemversion",osVersion);
sendBroadcast(closeintent);

 

 

接受廣播:

 public class receiveBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            checkZipFileResult = intent.getStringExtra("checkresult");	
            currentVersion = intent.getStringExtra("currentversion");
            osVersion = intent.getStringExtra("systemversion");
            if(checkZipFileResult.equals("true")) {
                try {
                    imageTimesTamp = intent.getStringExtra("imagetimestamp");
                    offset = Long.parseLong(intent.getStringExtra("offset"));
                    size =  Long.parseLong(intent.getStringExtra("size"));
                    updatePath = intent.getStringExtra("updatepath");
                    headerKeyValuePairs = intent.getStringArrayExtra("header");
                    mWindowManagerFuncs.systemUpdate(currentVersion, osVersion);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else{	
                mWindowManagerFuncs.showReadFileFailPromptDialog();
            }	
        }
    }	

 

 

需要在/frameworks/base/core/res/AndroidManifest.xml 新增:

 

你可能感兴趣的:(在android frameworks裏新增廣播接受的注意事項)