3

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

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

目标 API 为 16 (4.1):

MainActivity(安全的虚拟活动):

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>
4

1 回答 1

1

If I'm not mistaken I think you need to switch

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

to

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

so it unregisters the receiver prior to it being destoryed.

于 2014-03-27T00:31:54.150 回答