1

我想創建一個可以通過駕駛按鈕激活的駕駛應用程序的應用程序或B)自動使用汽車停靠站。如何完成()如果在按下退出按鈕的情況下使用汽車之家時的活動?

我通過使用UiModeManager到enableCarMode:

//設置UI來車模式

UiModeManager UImanager = (UiModeManager)getSystemService(Context.UI_MODE_SERVICE); 
UImanager.enableCarMode(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME); 

我都沒有問題推我的應用程序退出按鈕的手動方法完成,但是,如果汽車之家被激活,那麼我的應用程序在後臺運行。這是我需要的。

但是,一旦我在仿真模式下使用汽車之家的退出按鈕,我將獲得Android主屏幕,並且我的應用程序將在後臺運行。即:除非回到應用程序並關閉應用程序,否則無法關閉應用程序。

如果我在啓動汽車之家用戶界面後完成我的應用程序,那麼我已經失去了我的應用程序。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.HelloWord333" android:versionCode="1" 
    android:versionName="1.0.0"> 
<uses-sdk android:minSdkVersion="8" /> 


<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".BBM" android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.CAR_DOCK" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <receiver android:name=".Driving"> 
     <intent-filter> 
      <action android:name="android.app.action.EXIT_CAR_MODE" /> 
     </intent-filter> 
    </receiver> 

</application> 

有一次,我收到的廣播接收器,我不能回到我的主要活動。我試圖使用意圖,但沒有運氣調用我的主要活動。

package com.HelloWorld333; 

import android.app.Activity; 
import android.app.PendingIntent; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.telephony.SmsManager; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
import android.app.UiModeManager; 

public class Driving extends BroadcastReceiver 
{ 
    static final String CarDuck = "android.intent.category.CAR_DOCK"; 
    static final String CarMode = "android.intent.category.CATEGORY_CAR_MODE"; 
    public static String ACTION_EXIT_CAR_MODE = "android.app.action.EXIT_CAR_MODE"; 




    @Override 
    public void onReceive(Context context, Intent intent) 
    { 


if (intent.getAction().equals(ACTION_EXIT_CAR_MODE)){ 

Toast.makeText(context.getApplicationContext(),"Houston we have a Exit Car mode", Toast.LENGTH_SHORT).show(); 

ExitFromCarMode = true; 

UiModeManager UImanager = (UiModeManager)context.getSystemService(Context.UI_MODE_SERVICE);    
      UImanager.disableCarMode(UiModeManager.DISABLE_CAR_MODE_GO_HOME); 

      //Context context1 = context.getApplicationContext(); 

//I'm sorry I have tried but no luck in trying to reach my main Activity 

      // Intent intent1 = new Intent(context1, ExitCarMode.class); 
      // intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
      // context1.startActivity(intent1);    
    } 
} 

我試圖用廣播接收方法,在我的主要活動類,但在運行時,應用程序崩潰,一旦汽車模式的退出按鈕被按下。

我很感激任何幫助,並期待任何提示。

我已經在圈子裏已經持續這個過去兩天了,相信我,這是沒有樂趣,當你錯過了曲棍球比賽:(。

+0

logcat對崩潰說的是什麼? – Stephan 2011-04-20 05:03:05

回答

1

我也有類似的問題,在BroadcastReceiver崩潰。原來startActivity要求你在非活動上下文中調用時添加FLAG_ACTIVITY_NEW_TASK標誌(請參閱Docs中的隱藏註釋)。

相關問題