2

ヘッドセットが接続された後に通知を表示し、プラグを抜いた後に非表示にしたい。したがって、BOOT_COMPLETED でトリガーされる BroadCastReceiver に ACTION_HEADSET_PLUG インテント リスナーを登録します。私の場合、HTC Sense (HTC One S) を使用しており、Sense の読み込み中にヘッドセットを接続した場合 (WaitDialog) にのみ通知が表示され、その後は表示されなくなります。

「new OnBootreceiver().onReceive(this, null)」を介して実行時にアプリで OnBootListener をトリガーすると、完璧に動作します。

マニフェスト:

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

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        ...
    </activity>

    <receiver android:name=".OnBootReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
 ...

OnBootReceiver:

public void onReceive(Context context, Intent intent) {
    IntentFilter receiverFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
    HeadsetStateReceiver receiver = new HeadsetStateReceiver();
    context.registerReceiver( receiver, receiverFilter );
}

ヘッドセット状態レシーバー:

public void onReceive(Context context, Intent intent) {
    Toast.makeText(context,
            "" + (Integer) (intent.getExtras().get("state")),
            Toast.LENGTH_SHORT).show();
    mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Log.e(this.getClass().toString(), "Intent Headset_plug received "
            + intent);
    if ((Integer) (intent.getExtras().get("state")) != 0) {
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        notification.contentIntent = PendingIntent.getActivity(
                context.getApplicationContext(), 0, new Intent(), 0);
        notification.setLatestEventInfo(context, title, message,
                notification.contentIntent);
        mNotificationManager.notify(0xBEA15, notification);
    } else {
        mNotificationManager.cancelAll();
    }

}
4

2 に答える 2

2

なぜ BOOT_COMPLETED レシーバーが必要なのですか? マニフェスト ファイルを使用して ACTION_HEADSET_PLUG を受信しようとしましたか?

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
        ... 
    </activity> 

    <receiver android:name=".HeadsetStateReceiver" > 
        <intent-filter> 
            <action android:name="android.intent.ACTION_HEADSET_PLUG" /> 
        </intent-filter> 
    </receiver> 

あなたのコードも少し乱雑です:

public void onReceive(Context context, Intent intent) {
    boolean isConnected = intent.getIntExtra("state",0)==1; // Can also be 2 if headset is attached w/o mic.

    Toast.makeText(context, 
            isConnected?"connected":"disconnected"), 
            Toast.LENGTH_SHORT).show(); 
    mNotificationManager = (NotificationManager) context 
        .getSystemService(Context.NOTIFICATION_SERVICE); 

    if (isConnected) {
        Log.e(this.getClass().toString(), "Intent Headset_plug connected");
        notification.flags = Notification.FLAG_ONGOING_EVENT; 
        notification.contentIntent = PendingIntent.getActivity( 
                context.getApplicationContext(), 0, new Intent(), 0); 
        notification.setLatestEventInfo(context, title, message, 
                notification.contentIntent); 
        mNotificationManager.notify(0xBEA15, notification); 
    } else {
        Log.e(this.getClass().toString(), "Intent Headset_plug disconnected");
        mNotificationManager.cancelAll(); 
    } 
} 


わかりましたダイレクトレシーバーは機能しません(ここに良い説明があります)。

受信機がしばらくすると機能しなくなる理由がわかりました。アプリケーションは、システムによってクリーンアップされている必要があります。それを機能させるには、アプリケーションがクリーンアップされないようにする何かをしなければなりません。そのため、サービスを作成する必要があります。BOOT_COMPLETED でサービスを開始し、このサービスで ACTION_HEADSET_PLUG のブロードキャスト レシーバーを登録します。これにより、アプリケーションがクリーンアップされないようにする必要があります。

于 2012-07-16T10:35:46.737 に答える
1

あなたのコードは正しいですが、私が知る限り、マニフェストに HEADSET_PLUG フィルターを配置することはできません。代わりに、レシーバーをマニフェストから削除し、アプリの一部からプログラムで実行します。メイン アクティビティの onCreate() メソッドは問題ないはずです。次に例を示します。

ReceiveBroadcast receiver=new ReceiveBroadcast();       
registerReceiver(receiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG)); 
于 2015-03-19T15:16:28.753 に答える