手机从汽车底座中取出后,应用程序不会从抽屉中启动

发布于 2024-08-30 16:54:40 字数 1621 浏览 4 评论 0原文

我正在为 Android 2.0+ 设备创建替代 Car Home 应用程序。该应用程序需要在手机插入汽车底座时启动,并在从底座中取出时终止。它还需要能够从应用程序抽屉启动。

我现在遇到一个问题,一旦将手机插入扩展坞并从扩展坞中取出,我就无法再从应用程序抽屉启动应用程序,因为每次启动应用程序时,我的 BroadcastReceiver 都会因某种原因拾取 DOCK_EVENT 操作。我创建了一个仅注册我的 BroadcastReceiver 的测试项目,并且发生了同样的事情。

下面是 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();
        }
    }
}

}

我的主 Activity 如下:

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();
}

}

主 Activity 有一个针对 action.MAIN、category.LAUNCHER、category.DEFAULT 和category.CAR_DOCK 的 Intent 过滤器。关于为什么会发生这种情况有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡渣熟男 2024-09-06 16:54:40

Intent.ACTION_DOCK_EVENT 是粘性广播。这意味着,当您为其注册接收器时,您将立即获取该操作的最后广播Intent,然后获取所有后续广播,直到它被取消注册。除了找到处理这种情况的方法之外,你对此无能为力。

顺便说一句,我建议在 IntentFilter 中使用 Intent.ACTION_DOCK_EVENT 而不是 "android.intent.action.DOCK_EVENT"。这样,如果由于某种愚蠢的原因他们更改了实际的字符串,您的代码不需要更改。

Intent.ACTION_DOCK_EVENT is a sticky broadcast. This means that when you register a receiver for it, you will immediately get the last-broadcast Intent for that action, then all subsequent broadcasts until it is unregistered. There is nothing much you can do about it, other than finding a way of dealing with the situation.

BTW, I recommend using Intent.ACTION_DOCK_EVENT rather than "android.intent.action.DOCK_EVENT" in your IntentFilter. This way, if for some goofy reason they change the actual string, your code does not need to change.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文