问题
So my app shows a notification icon on headset plugin, and it destroys itself also if the headset is removed. But that only works if the application has been opened. How could I make it so it detects the headphone plugin and runs the icon in the background? Here is the class the detects if the headset has been plugged in or not
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);
}
}
I think you have to make a receiver or something like that so I did, but I'm really new to this stuff so I'm not really sure what it all is and stuff. This is most of my manifest which includes the receievers and permissions
<?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" />
This is the last thing I have to do to finish my application so if you could help me or guide me in the right direction it would mean a lot. Thank you very much.
来源:https://stackoverflow.com/questions/19368767/how-to-call-this-even-when-the-app-hasnt-even-started