gpt4 book ai didi

android - 使此代码在启动时工作

转载 作者:行者123 更新时间:2023-11-29 21:30:44 25 4
gpt4 key购买 nike

我已经坚持了几个星期了……我无法让它工作。我想要做的是让我的应用程序识别用户何时将耳机/头戴式耳机插入手机,以及何时插入,以便它显示通知图标。这是我的课。我已经添加了

 public class ItudeMobileBroadcastReceiver extends BroadcastReceiver {
int NOTIFICATION_ID = 1234567890;
NotificationManager mNotificationManager;

@Override
public void onReceive(Context context, Intent intent) {
/* Only perform this code if the BroadcastReceiver received the ACTION_BOOT_COMPLETED action.*/
Log.d("Autobooted", "it booted");
Toast laToast = Toast.makeText(context, "I have booted", 5);
laToast.show();
//It boots so it shows that toast. Rest doesn't work though
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 Title";
Notification n = new Notification(R.drawable.notification_icon, "Notification Title", 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, "Notification Title", "Bottom Line", pending);
mNotificationManager.notify(NOTIFICATION_ID, n);
}
}

这是我的 list

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

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

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

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.example.testreciever.BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:enabled="true"
android:name=".ItudeMobileBroadcastReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">

<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ACTION_HEADSET_PLUG" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.MODIFY_AUDIO_SETTINGS" />
</intent-filter>

</receiver>

<activity
android:name="com.example.testreciever.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.testreciever.Hope"
android:label="@string/title_activity_hope" >
</activity>
</application>

</manifest>

在启动时它确实显示“它已启动” toast ,所以我知道它正在工作。但是当我插入耳机时它没有显示通知图标。我真的不知道该怎么办了。如果你能帮忙,请帮忙。非常感谢。

我知道通知代码有效,因为如果我将它放在 Activity 类中并启动应用程序,它就可以工作,但我不想启动应用程序。我只是想让它在启动时工作。

最佳答案

HeadsetObserver类负责在耳机插入/拔出时广播 Intent.ACTION_HEADSET_PLUG。以下是 HeadsetObserver.sendIntent()

中的部分代码
        //  Pack up the values and broadcast them to everyone
Intent intent = new Intent(Intent.ACTION_HEADSET_PLUG);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
int state = 0;
int microphone = 0;

if ((headset & HEADSETS_WITH_MIC) != 0) {
microphone = 1;
}
if ((headsetState & headset) != 0) {
state = 1;
}
intent.putExtra("state", state);
intent.putExtra("name", headsetName);
intent.putExtra("microphone", microphone);

if (LOG) Slog.v(TAG, "Intent.ACTION_HEADSET_PLUG: state: "+state+" name: "+headsetName+" mic: "+microphone);
// TODO: Should we require a permission?
ActivityManagerNative.broadcastStickyIntent(intent, null);

如您所见, Intent 标记为 Intent.FLAG_RECEIVER_REGISTERED_ONLY .这意味着您的 AndroidManifest.xml 中定义的 BroadcastReceiver 无法接收广播。

您必须让应用程序的某些组件运行,即。 ServiceActivity,并使用 registerReceiver(BroadcastReceiver receiver, IntentFilter filter) 注册接收器

编辑,示例代码

正如我所说,您需要运行应用程序的某些组件才能接收 Intent.ACTION_HEADSET_PLUG 广播。最好的选择可能是 Service

所以让我们创建一个服务,HeadsetObserverService.java:

public class HeadsetObserverService extends Service {

private static final String TAG = "HeadsetObserverService";

@Override
public IBinder onBind(Intent arg0) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

// Create a filter. We are interested in the Intent.ACTION_HEADSET_PLUG action
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_HEADSET_PLUG);

// Register a new HeadsetReceiver
registerReceiver(new HeadsetReceiver(), filter);

// We return START_STICKY. If our service gets destroyed, Android will try to restart it when resources are available.
return START_STICKY;
}

private class HeadsetReceiver extends BroadcastReceiver {

@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");

// Cancel notification here

break;
case 1:
Log.d("plugged", "Headset is plugged");

// Show notification

break;
default:
Log.w("uh", "I have no idea what the headset state is");
}
}
}
}
}

如果您希望此服务在手机重启时自动启动,您需要一个BroadcastReceiver 监听Intent.ACTION_BOOT_COMPLETED广播。

public class BootReceiver extends BroadcastReceiver {

private static final String TAG = "BootReceiver";

@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive() - Received broadcast");

if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Log.i(TAG, "onReceive() - Intent action: " + Intent.ACTION_BOOT_COMPLETED);
Log.i(TAG, "onReceive() - Starting service..");

// Phone is booted, lets start the service
Intent serviceIntent = new Intent(context, HeadsetObserverService.class);
context.startService(serviceIntent);
}
}
}

您需要将新的ServiceBootReceiver 添加到您的AndroidManifest

<receiver 
android:name=".BootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

<service android:name=".HeadsetObserverService">

您还应该有一个 Activity,用户可以在其中启动和停止 HeadsetObserverService

关于android - 使此代码在启动时工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19621860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com