简体   繁体   English

在Android中启动Service并通过多个活动与之通信

[英]Start Service in Android and communicate with it from multiple Activities

Well I would like to communicate to a Service from multiple Activities, like everyone listen to a broadcast channel and update their Twitter statuses .. 好吧,我想通过多个活动与服务进行通信,就像每个人都收听广播频道并更新其Twitter状态一样。

My recent effort is to use Broadcast Receiver on each Activity to receive the broadcast sent from the Service. 我最近的工作是在每个活动上使用广播接收器来接收从服务发送的广播。 But I would like to know that it's a good way or not and what is the property way? 但是我想知道这是否是一个好方法,什么是财产方法?

Furthermore, I would like to ask if i should use Binder to start this kind of service or use startService method (I am a beginner in using Service actually..). 此外,我想问我是否应该使用Binder启动这种服务或使用startService方法(我实际上是使用Service的初学者。)。

Thanks in advance. 提前致谢。

        OKay you are a beginner in Service means Refer this below Link 

http://developer.android.com/guide/components/services.html http://developer.android.com/guide/components/services.html

        in your Question while you using service you must register in Manifest file..
        like this..

        for example i did unlock screen using service and broadcast receiver.

        <service
                    android:name="com.services.Unlock_Service"
                    android:enabled="true"
                    android:icon="@drawable/ic_launcher" >
                </service>

        unlockservice.java



        package com.services;

        import java.util.List;

        import android.annotation.SuppressLint;
        import android.app.Activity;
        import android.app.ActivityManager;
        import android.app.KeyguardManager;
        import android.app.Service;
        import android.app.KeyguardManager.KeyguardLock;
        import android.content.BroadcastReceiver;
        import android.content.ComponentName;
        import android.content.Context;
        import android.content.Intent;
        import android.content.IntentFilter;
        import android.os.IBinder;
        import android.os.PowerManager;
        import android.os.PowerManager.WakeLock;
        import android.util.Log;
        import android.widget.Toast;

        @SuppressWarnings("deprecation")
        public class Unlock_Service extends Service {
            ScreenBroadcastReceiver m_receiver;
            public String activityname;
            PowerManager pm;
            WakeLock wl;

            @Override
            public void onCreate() {
                IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
                filter.addAction(Intent.ACTION_SCREEN_OFF);
                m_receiver = new ScreenBroadcastReceiver();
                registerReceiver(m_receiver, filter);
                Log.d("Widgettool", "works");
            }

            @Override
            public int onStartCommand(Intent intent, int flags, int startId) {
                start();
                Toast.makeText(getBaseContext(), "SERVICE ON", Toast.LENGTH_SHORT)
                        .show();

                return START_STICKY;
            }

            @Override
            public void onDestroy() {
                stop();
                unregisterReceiver(m_receiver);
            }

            @SuppressLint("Wakelock")
            public void start() {
                try {

                    pm = (PowerManager) getSystemService(POWER_SERVICE);
                    wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                            | PowerManager.ACQUIRE_CAUSES_WAKEUP, ".SensorActivity");
                    wl.acquire();
                    KeyguardManager mgr = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
                    KeyguardLock lock = mgr.newKeyguardLock(KEYGUARD_SERVICE);
                    lock.disableKeyguard();
                    // new TabActivityPacs().service();
                } catch (Exception e) {

                }

            }

            public void stop() {

                ActivityManager am = (ActivityManager) this
                        .getSystemService(ACTIVITY_SERVICE);
                List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
                Log.d("topActivity", "CURRENT Activity ::"
                        + taskInfo.get(0).topActivity.getClassName());
                activityname = taskInfo.get(0).topActivity.getClassName();
                ComponentName componentInfo = taskInfo.get(0).topActivity;
                componentInfo.getPackageName();

            }

            private class ScreenBroadcastReceiver extends BroadcastReceiver {

                @Override
                public void onReceive(Context context, Intent intent) {
                    if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                        Log.d("ON SCREEN ON", "might hang here");
                        // start();
                        stop();

                    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                        start();
                        Log.d("SCREEN OFF", "might hang here");
                    }
                }

            }

            @Override
            public IBinder onBind(Intent intent) {

                return null;
            }

        }


    in your question... you have to use broadcast receiver in all activity then only you receive your corresponding information using service.. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM