gpt4 book ai didi

Android - 如果设备在注册接收器的情况下重新启动,我的后台服务注册的广播接收器无法关闭

转载 作者:太空狗 更新时间:2023-10-29 14:18:07 24 4
gpt4 key购买 nike

我正在尝试创建一个小应用程序来监听我的蓝牙媒体接收器的按下命令并在按下时启动 Tasker 任务。使用我从 tasker 发送的 Intent 启动和停止该服务。

一切都运行完美,直到我重新启动我的设备(或断电),而我的服务接收器仍然注册。一旦我的设备重新启动,接收器将保持注册状态,如果我尝试使用我的 STOP Intent 取消注册它,我的应用程序会崩溃。如果我的手机即将关机,我该如何注销我的接收器?

目标 API 是 16 (4.1):

MainActivity(安全虚拟 Activity ):

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
finish();

}
}

远程控制接收器

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class RemoteControlReceiver extends BroadcastReceiver {

public RemoteControlReceiver () {
super();
}


@Override
public void onReceive(Context context, Intent intent) {


intent.setClass(context, RemoteControlService.class);
context.startService(intent);

}


}

远程控制服务

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Handler;
import android.os.IBinder;
import android.view.KeyEvent;
import com.example.simplemediabuttonlistener.TaskerIntent;

public class RemoteControlService extends Service {
public RemoteControlService() {
}

private Handler handler;

//sets up the audio manager and names the receiver component (registered later)

AudioManager manager;

ComponentName mReciever = new ComponentName(RemoteControlReceiver.class.getPackage().getName(), RemoteControlReceiver.class.getName());


@Override
public void onCreate(){
super.onCreate();
}

@Override
public void onDestroy(){
super.onDestroy();
manager.unregisterMediaButtonEventReceiver(mReciever);
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler = new Handler();
String intentAction = intent.getAction();

//if we are using our custom start intent
if (intentAction == "com.example.simplemediabuttonlistener.START"){
//we have launched from tasker...
//so register our receiver
manager = (AudioManager) getSystemService(AUDIO_SERVICE);
manager.registerMediaButtonEventReceiver(mReciever);
}

//if we are using our custom stop intent
if (intentAction == "com.example.simplemediabuttonlistener.STOP"){
//we have stopped from tasker...

//so stop service
stopSelf();
}

//if a media button is pressed
if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
KeyEvent event = (KeyEvent) intent
.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

if (event == null) {
return START_STICKY;
}

int keycode = event.getKeyCode();
int action = event.getAction();


//check which button it is and run the appropriate task
if (keycode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
|| keycode == KeyEvent.KEYCODE_MEDIA_PLAY
|| keycode == KeyEvent.KEYCODE_MEDIA_PAUSE
|| keycode == KeyEvent.KEYCODE_HEADSETHOOK) {

if (action == KeyEvent.ACTION_DOWN) {

handler.post(new Runnable() {

public void run() {

if ( TaskerIntent.testStatus( getApplicationContext() ).equals( TaskerIntent.Status.OK ) ) {
TaskerIntent i = new TaskerIntent( "BTPLAY" );
getApplicationContext().sendBroadcast( i );
}
}
});

}
}

if (keycode == KeyEvent.KEYCODE_MEDIA_NEXT) {

if (action == KeyEvent.ACTION_DOWN) {
// Start your app here!
handler.post(new Runnable() {

public void run() {

if ( TaskerIntent.testStatus( getApplicationContext() ).equals( TaskerIntent.Status.OK ) ) {
TaskerIntent i = new TaskerIntent( "BTNEXT" );
getApplicationContext().sendBroadcast( i );
}
}
});

}
}
}

return START_STICKY;
}

@Override
public IBinder onBind(Intent arg0) {
// We dont bind to an activity, so this is unused
return null;
}

}

AndroidManifest

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

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

<permission android:name = "net.dinglisch.android.tasker.PERMISSION_RUN_TASKS" />
<permission android:name = "android.permission.BLUETOOTH" />


<uses-permission android:name = "android.permission.BLUETOOTH" />
<uses-permission android:name = "net.dinglisch.android.tasker.PERMISSION_RUN_TASKS" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<receiver android:name=".RemoteControlReceiver" >
<intent-filter android:priority="1000000000" >
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>

<activity
android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>

<service
android:name=".RemoteControlService">
<intent-filter>

<action android:name= "com.example.simplemediabuttonlistener.START">
<category android:name = "android.intent.category.DEFAULT" />
</action>

<action android:name= "com.example.simplemediabuttonlistener.STOP">
<category android:name = "android.intent.category.DEFAULT" />
</action>
</intent-filter>
</service>
</application>
</manifest>

最佳答案

如果我没记错的话,我认为你需要切换

@Override
public void onDestroy(){
super.onDestroy();
manager.unregisterMediaButtonEventReceiver(mReciever);
}

@Override
public void onDestroy(){
manager.unregisterMediaButtonEventReceiver(mReciever);
super.onDestroy();
}

因此它会在接收器被销毁之前注销接收器。

关于Android - 如果设备在注册接收器的情况下重新启动,我的后台服务注册的广播接收器无法关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19498163/

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