I am creating a replacement Car Home app for Android 2.0+ devices. The app needs to launch when the phone is inserted into the car dock, as well as terminate when it is removed from the dock. It also needs to be able to be launched from the app drawer.
I'm having a problem right now where once the phone is inserted and removed from the dock, I can no longer launch the app from the app drawer because every time I launch the app my BroadcastReceiver picks up a DOCK_EVENT action for some reason. I created a test project that only registers my BroadcastReceiver, and the same thing happens.
Here's the code for the BroadcastReceiver:
public class CarDockBroadcastReceiver extends BroadcastReceiver {
/**
* @see android.content.BroadcastReceiver#onReceive(Context,Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
// TODO Put your code here
if(intent.getExtras().containsKey("android.intent.extra.DOCK_STATE")){
int state = intent.getExtras().getInt("android.intent.extra.DOCK_STATE",1);
if(state == 0){
Log.i("Dock", "Removed from dock!");
((Activity)context).finish();
}
}
}
}
My main Activity is as follows:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
CarDockBroadcastReceiver receiver;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter = new IntentFilter("android.intent.action.DOCK_EVENT");
receiver = new CarDockBroadcastReceiver();
registerReceiver(receiver, filter);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
unregisterReceiver(receiver);
super.onDestroy();
}
}
The main Activity has an intent filter for action.MAIN, category.LAUNCHER, category.DEFAULT, and category.CAR_DOCK. Any ideas on why this is happening?