The snippet:
---------------------------- Receiver body --------------------------------------------
private String ACTION_CUSTOM_HOTKEY = "android.internal.policy.impl.PhoneWindowManager";
BroadcastReceiver mDockReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
...
else if (ACTION_CUSTOM_HOTKEY.equals(intent.getAction())) {
String key = intent.getExtras().getString("whichkey");
String appName = intent.getExtras().getString(key);
Log.d(TAG, "write pair " + key + "-" + appName);
SharedPreferences.Editor editor = mKeyAppMap.edit();
editor.putString(key, appName);
editor.commit();
}
...
};
---------------------------- Register the receiver and filter ----------------------------
IntentFilter filter = new IntentFilter();
...
filter.addAction(ACTION_CUSTOM_HOTKEY);
Intent intent = context.registerReceiver(mDockReceiver, filter);
if (intent != null) {
// Retrieve current sticky dock event broadcast.
mDockMode = intent.getIntExtra(Intent.EXTRA_DOCK_STATE,
Intent.EXTRA_DOCK_STATE_UNDOCKED);
}
...
-----------------------------------------------------------------------------------
Instead of using registerReceiver, one could also add the filter it in the manifest file for permitted intent action.
-----------------------------------------------------------------------------------
----------------------- Send broadcasting message -------------------------------
Intent intent = new Intent();
intent.setAction("android.internal.policy.impl.PhoneWindowManager");
intent.putExtra("whichkey", currentTab);
intent.putExtra(currentTab, entry.info.packageName);
getActivity().sendBroadcast(intent);
Reference links
http://stackoverflow.com/questions/3907713/how-to-send-and-receive-broadcast-message
http://www.techotopia.com/index.php/Android_Broadcast_Intents_and_Broadcast_Receivers
http://www.ozzysun.com/2010/11/android.html
沒有留言:
張貼留言