2017-06-22 176 views
0

我有一個耳機,耳機。它垂直方向有按鈕,增加音量,中間按鈕,音量較低。按耳機按鈕並觸發事件?

通過按中間按鈕,即使屏幕關閉或手機鎖定,它也會打開Spotify並播放音樂。

我想爲我的應用程序做這個,當按下這個中間按鈕時,這個應用程序觸發一個事件,有可能嗎?或者,按其中一個音量按鈕並觸發一個事件。

有可能嗎?如果是,是否有任何示例,源代碼,與此應用程序?

編輯:

的代碼沒有工作:

主營:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     IntentFilter filtro = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);//"android.intent.action.MEDIA_BUTTON" 
     MediaButtonIntentReceiver r = new MediaButtonIntentReceiver(); 
     filtro.setPriority(1000); //this line sets receiver priority 
     registerReceiver(r, filtro); 
    } 


} 

廣播:

public class MediaButtonIntentReceiver extends BroadcastReceiver { 
    public MediaButtonIntentReceiver() { 
     super(); 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     String intentAction = intent.getAction(); 
     if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { 
      return; 
     } 
     KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); 
     if (event == null) { 
      return; 
     } 
     int action = event.getAction(); 
     if (action == KeyEvent.ACTION_DOWN) { 
      toDo(); 
      Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show(); 
     } 
     abortBroadcast(); 
    } 

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.aula.bob.headset"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver android:name=".MediaButtonIntentReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.MEDIA_BUTTON" /> 
      </intent-filter> 
     </receiver> 

    </application> 

</manifest> 

回答

1

創建您的應用程序擴展廣播接收器類:

public class MediaButtonIntentReceiver extends BroadcastReceiver { 

public MediaButtonIntentReceiver() { 
super(); 
} 

@Override 
public void onReceive(Context context, Intent intent) { 
String intentAction = intent.getAction(); 
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) { 
    return; 
} 
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); 
if (event == null) { 
    return; 
} 
int action = event.getAction(); 
if (action == KeyEvent.ACTION_DOWN) { 
// do something 
    Toast.makeText(context, "BUTTON PRESSED!", Toast.LENGTH_SHORT).show(); 
} 
abortBroadcast(); 
} 
} 

而在你的活動課:

public class MainActivity extends Activity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);//"android.intent.action.MEDIA_BUTTON" 
MediaButtonIntentReceiver r = new MediaButtonIntentReceiver(); 
filter.setPriority(10000); //this line sets receiver priority 
registerReceiver(r, filter); 

} 
} 

定義你的接收器在Android清單如下:

<receiver android:name=".MediaButtonIntentReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.MEDIA_BUTTON" /> 
    </intent-filter> 
</receiver> 

希望它會爲你工作!

+0

這也適用於關閉應用程序或至少在後臺? –

+0

是的,它應該。 –

+0

沒有爲我工作。 我會編輯問題。 –