Android:如何在服务

时间:2016-06-18 05:19:10

标签: android android-service localbroadcastmanager

我有服务,我希望有一个localbroadcast接收器。我想从活动中获取一个值来创建我的服务。

注意:我可以使用intent从onStartCommand()中获取活动值到我的服务。但是在这里我需要来自服务onCreate中的活动的值。

以下是我的服务文件:

    public class MediaServiceSimha extends Service {

        private MediaPlayer player;
        String musicpath;
        private ResponseReceiver receiver;

        public MediaServiceSimha() {}

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

        @Override
        public void onDestroy() {
            LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
            localBroadcastManager.unregisterReceiver(receiver);
            super.onDestroy();
        }

        @Override
        public boolean onUnbind(Intent intent) {
            return super.onUnbind(intent);
        }

        @
        Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            player.start();
            return super.onStartCommand(intent, flags, startId);
        }

        @Override
        public void onCreate() {
            super.onCreate();
            IntentFilter broadcastFilter = new IntentFilter("com.example.myintentserviceapp.intent_service.ALL_DONE");
            receiver = new ResponseReceiver();
            LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
            localBroadcastManager.registerReceiver(receiver, broadcastFilter);
        }

        public class ResponseReceiver extends BroadcastReceiver {

            @Override
            public void onReceive(Context context, Intent intent) {
                musicpath = intent.getStringExtra("musicpath");
            }
        }
    }

以下是我的活动文件,我想传递一个名为musicpath的值

public class ServiceTest extends AppCompatActivity {
    Intent intent;

    public static final String TEXT_INPUT = "inText";
    //MediaServiceSimha mediaServiceSimha;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        L.m("activity_oncreate_starting");
        setContentView(R.layout.activity_service_test);
        intent= new Intent(this,MediaServiceSimha.class);

        startService(intent);

        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("com.example.myintentserviceapp.intent_service.ALL_DONE");
        broadcastIntent.putExtra("musicpath", "/storage/emulated/0/Download/1.mp3");
        LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
        localBroadcastManager.sendBroadcast(broadcastIntent)
    }


}

运行后,它表示musicpath值为空。

怎么做?

2 个答案:

答案 0 :(得分:0)

我的目标是将变量值直接传递给服务类中的oncreate。

我尝试了LocalBroadcast的reciver和onbindservices - onServiceConnected

他们两个我发现他们只会被执行直到创造 - > onbind / onstartcommand在服务中执行。

因此,只剩下选项是onstartcommand()

因此,当我们使用intent传递值时,可以立即在onstart命令中使用它们。

答案 1 :(得分:-1)

当您呼叫启动服务时,并不意味着它会立即呼叫,所以等待服务的创建,无论何时您的服务启动,您都可以向您的活动发送回叫,然后您可以向您的服务广播值

06-18 02:15:19.740: D/dalvikvm(889): Not late-enabling CheckJNI (already on)
06-18 02:15:22.650: D/AndroidRuntime(889): Shutting down VM
06-18 02:15:22.650: W/dalvikvm(889): threadid=1: thread exiting with uncaught exception (group=0xb4aa0ba8)
06-18 02:15:22.690: E/AndroidRuntime(889): FATAL EXCEPTION: main
06-18 02:15:22.690: E/AndroidRuntime(889): Process: com.example.j, PID: 889
06-18 02:15:22.690: E/AndroidRuntime(889): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.j/com.example.j.MainActivity}: java.lang.NullPointerException
06-18 02:15:22.690: E/AndroidRuntime(889):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-18 02:15:22.690: E/AndroidRuntime(889):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-18 02:15:22.690: E/AndroidRuntime(889):  at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-18 02:15:22.690: E/AndroidRuntime(889):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-18 02:15:22.690: E/AndroidRuntime(889):  at android.os.Handler.dispatchMessage(Handler.java:102)
06-18 02:15:22.690: E/AndroidRuntime(889):  at android.os.Looper.loop(Looper.java:136)
06-18 02:15:22.690: E/AndroidRuntime(889):  at android.app.ActivityThread.main(ActivityThread.java:5017)
06-18 02:15:22.690: E/AndroidRuntime(889):  at java.lang.reflect.Method.invokeNative(Native Method)
06-18 02:15:22.690: E/AndroidRuntime(889):  at java.lang.reflect.Method.invoke(Method.java:515)
06-18 02:15:22.690: E/AndroidRuntime(889):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-18 02:15:22.690: E/AndroidRuntime(889):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-18 02:15:22.690: E/AndroidRuntime(889):  at dalvik.system.NativeStart.main(Native Method)
06-18 02:15:22.690: E/AndroidRuntime(889): Caused by: java.lang.NullPointerException
06-18 02:15:22.690: E/AndroidRuntime(889):  at com.example.j.MainActivity.onCreate(MainActivity.java:25)
06-18 02:15:22.690: E/AndroidRuntime(889):  at android.app.Activity.performCreate(Activity.java:5231)
06-18 02:15:22.690: E/AndroidRuntime(889):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-18 02:15:22.690: E/AndroidRuntime(889):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-18 02:15:22.690: E/AndroidRuntime(889):  ... 11 more