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