2010-10-11 7 views
1

Я пишу приложение для Android для автоматического ответа на входящие звонки. Я использую приемник и пытаюсь отправить событие ACTION_MEDIA_BUTTON, когда телефон звонит.ACTION_MEDIA_BUTTON не работает на реальном устройстве

Все работает отлично на эмуляторе - Когда телефон звонит, он фактически отвечает на звонок автоматически.

Но когда я пытаюсь использовать его на устройстве, он просто не работает (HTC Legend).

Это код:

// trigger on buttonUp instead of buttonDown 
Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);    
buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK)); 
context.sendOrderedBroadcast(buttonDown , "android.permission.CALL_PRIVILEGED"); 

// trigger on buttonUp instead of buttonDown 
Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);    
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK)); 
context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED"); 

И это код приемника в файле XML:

<receiver android:name=".PhoneOutgoingCallReceiver" android:enabled="true"> 
    <intent-filter android:priority="0"> 
    <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
    </intent-filter> 
</receiver> 
<receiver android:name=".PhoneIncomingCallReceiver" android:enabled="true"> 
    <intent-filter android:priority="0"> 
    <category android:name="android.intent.category.DEFAULT" /> 
    <action android:name="android.intent.action.PHONE_STATE" /> 
    <action android:name="android.intent.action.MEDIA_BUTTON" /> 
    </intent-filter> 
</receiver>` 

Кто знает, почему он не работает на реальном устройстве? Какая разница?

ответ

1

Просьба назвать этот метод. Он работает в моем htc wildfire s.

public static void answerPhoneHeadsethook(Context context) { 

    try { 
     Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON); 
     buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
       KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK)); 
     context.sendOrderedBroadcast(buttonDown, 
       "android.permission.CALL_PRIVILEGED"); 
     buttonDown = null; 
    } catch (Exception e) { 
     Toast.makeText(context, "Error First Try: " + e.getMessage(), 
       Toast.LENGTH_LONG).show(); 
    } 

    try { 
     // froyo and beyond trigger on buttonUp instead of buttonDown 
     Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON); 
     buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
       KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK)); 
     context.sendOrderedBroadcast(buttonUp, 
       "android.permission.CALL_PRIVILEGED"); 
     buttonUp = null; 
    } catch (Exception e) { 
     Log.e(" **** Error in Second Try: ", "" + e.getMessage()); 
     Toast.makeText(context, "Error Second Try: " + e.getMessage(), 
       Toast.LENGTH_LONG).show(); 
    } 

    try { 
     Intent headSetUnPluggedintent = new Intent(
       Intent.ACTION_HEADSET_PLUG); 
     headSetUnPluggedintent 
       .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); 
     headSetUnPluggedintent.putExtra("state", 1); // 0 = unplugged 1 = 
                 // Headset with 
                 // microphone 2 = 
                 // Headset without 
                 // microphone 
     headSetUnPluggedintent.putExtra("name", "Headset"); 
     // TODO: Should we require a permission? 
     context.sendOrderedBroadcast(headSetUnPluggedintent, null); 
     headSetUnPluggedintent = null; 
    } catch (Exception e) { 
     Log.e(" **** Error in Third Try: ", "" + e.getMessage()); 
     Toast.makeText(context, "Error Third Try: " + e.getMessage(), 
       Toast.LENGTH_LONG).show(); 
    } 

    // Second Time call for HTC Mobile 

    try { 
     // SettingsClass.logMe(tag, "Simulating headset button"); 
     Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON); 
     buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
       KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK)); 
     context.sendOrderedBroadcast(buttonDown, 
       "android.permission.CALL_PRIVILEGED"); 
     buttonDown = null; 
    } catch (Exception e) { 
     Log.e(" **** Error in First Try: ", "" + e.getMessage()); 
     Toast.makeText(context, "Error First Try: " + e.getMessage(), 
       Toast.LENGTH_LONG).show(); 
    } 

    try { 
     // froyo and beyond trigger on buttonUp instead of buttonDown 
     Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON); 
     buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(
       KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK)); 
     context.sendOrderedBroadcast(buttonUp, 
       "android.permission.CALL_PRIVILEGED"); 
     buttonUp = null; 
    } catch (Exception e) { 
     Log.e(" **** Error in Second Try: ", "" + e.getMessage()); 
     Toast.makeText(context, "Error Second Try: " + e.getMessage(), 
       Toast.LENGTH_LONG).show(); 
    } 

    try { 
     Intent headSetUnPluggedintent = new Intent(
       Intent.ACTION_HEADSET_PLUG); 
     headSetUnPluggedintent 
       .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); 
     headSetUnPluggedintent.putExtra("state", 0); // 0 = unplugged 1 = Headset with microphone 2 = Headset without microphone 
     headSetUnPluggedintent.putExtra("name", "Headset"); 
     // TODO: Should we require a permission? 
     context.sendOrderedBroadcast(headSetUnPluggedintent, null); 
     headSetUnPluggedintent = null; 
    } catch (Exception e) { 
     Log.e(" **** Error in Third Try: ", "" + e.getMessage()); 
     Toast.makeText(context, "Error Third Try: " + e.getMessage(), 
       Toast.LENGTH_LONG).show(); 
    } 

    android.os.Process.killProcess(android.os.Process.myPid()); 
} 
Смежные вопросы