0

そのため、私のアプリはヘッドセット プラグインに通知アイコンを表示し、ヘッドセットが取り外されるとアプリ自体も破棄されます。ただし、それはアプリケーションが開かれている場合にのみ機能します。ヘッドフォンプラグインを検出してバックグラウンドでアイコンを実行するようにするにはどうすればよいですか? ヘッドセットが接続されているかどうかを検出するクラスは次のとおりです

public class MusicIntentReceiver extends BroadcastReceiver {
    int NOTIFICATION_ID = 1234567890;
    NotificationManager mNotificationManager;
    @Override public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
            int state = intent.getIntExtra("state", -1);
            switch (state) {
            case 0:
                Log.d("unplugged", "Headset was unplugged");
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                notificationManager.cancel(NOTIFICATION_ID);
                Toast unpluggedToast = Toast.makeText(context, "Headset has been disconnected", Toast.LENGTH_SHORT);
                unpluggedToast.show();
                break;
            case 1:
                Log.d("plugged", "Headset is plugged");
                mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                showNotification(context, R.drawable.notification_icon, "short", false);
                Toast pluggedToast = Toast.makeText(context, "Headset has been connected", Toast.LENGTH_SHORT);
                pluggedToast.show();
                break;
            default:
                Log.d("uh", "I have no idea what the headset state is");
                Toast defaultToast = Toast.makeText(context, "No headset detected", Toast.LENGTH_SHORT);
                defaultToast.show();
            }
        }
    }

    private void showNotification(Context context, int statusBarIconID, String string, boolean showIconOnly) {
        Intent contentIntent = new Intent();
        //Text that it says in the notification area when the notification appears
        String dropDownText = "Notification";
        Notification n = new Notification(R.drawable.notification_icon, "Notification", System.currentTimeMillis());
        n.flags = Notification.FLAG_ONGOING_EVENT;
        Intent intent=new Intent(context,MainActivity.class); //Activity that will be launched if notification is clicked
        PendingIntent  pending=PendingIntent.getActivity(context, 0, intent, 0);
        n.setLatestEventInfo(context, "Notiication Icon", "Touch for more options", pending);
        mNotificationManager.notify(NOTIFICATION_ID, n);
    }
}

レシーバーかそれに類するものを作らなければならないと思うので作ったのですが、私はこのようなことに本当に慣れていないので、それが何なのかよくわかりません。これは、受信者と権限を含む私のマニフェストのほとんどです

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.notificationicon"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.ACTION_HEADSET_PLUG"/>
    <uses-permission android:name="android.permission.VIBRATE"/>

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver
    android:name=".MusicIntentReceiver"
    android:enabled="true"
    android:exported="true" >
    <intent-filter>
        <action android:name="android.intent.action.HEADSET_PLUG" />
    </intent-filter>
</receiver>
        <receiver android:name=".MusicIntentReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.HEADSET_PLUG" />
    </intent-filter>
</receiver>
        <receiver android:name=".MusicIntentReceiver" >
        <intent-filter>
             <action android:name="android.intent.action.BOOT_COMPLETED" />
             <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
        </receiver>
        <service
    android:name=".MusicIntentReceiever"
    android:enabled="true" /> 

これは、アプリケーションを完成させるために私がしなければならない最後のことです。あなたが私を助け、正しい方向に導くことができれば、それは大きな意味があります. どうもありがとうございました。

4

0 に答える 0