在Android中启动服务,并通过多种活动与之通信

在Android中启动服务,并通过多种活动与之通信,android,service,Android,Service,我想通过多种活动与服务交流,比如每个人都收听广播频道并更新他们的Twitter状态 我最近的工作是在每个活动上使用广播接收器来接收服务发送的广播。但我想知道,这是一个好办法还是不好,什么是财产办法 此外,我想问我是否应该使用Binder来启动这种服务,或者使用startService方法(实际上我是一个使用服务的初学者) 提前谢谢 OKay you are a beginner in Service means Refer this below Link 在您的问题中,当您使

我想通过多种活动与服务交流,比如每个人都收听广播频道并更新他们的Twitter状态

我最近的工作是在每个活动上使用广播接收器来接收服务发送的广播。但我想知道,这是一个好办法还是不好,什么是财产办法

此外,我想问我是否应该使用Binder来启动这种服务,或者使用startService方法(实际上我是一个使用服务的初学者)

提前谢谢

        OKay you are a beginner in Service means Refer this below Link 
在您的问题中,当您使用服务时,您必须在清单文件中注册。。
这样地。。
例如,我使用服务和广播接收器解锁屏幕。
unlockservice.java
包com.services;
导入java.util.List;
导入android.annotation.SuppressLint;
导入android.app.Activity;
导入android.app.ActivityManager;
导入android.app.KeyguardManager;
导入android.app.Service;
导入android.app.KeyguardManager.KeyguardLock;
导入android.content.BroadcastReceiver;
导入android.content.ComponentName;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.os.IBinder;
导入android.os.PowerManager;
导入android.os.PowerManager.WakeLock;
导入android.util.Log;
导入android.widget.Toast;
@抑制警告(“弃用”)
公共类解锁服务扩展服务{
屏幕广播接收机m_接收机;
公共字符串activityname;
PowerManager pm;
WakeLock wl;
@凌驾
public void onCreate(){
IntentFilter过滤器=新IntentFilter(Intent.ACTION屏幕打开);
filter.addAction(Intent.ACTION\u SCREEN\u OFF);
m_receiver=新的ScreenBroadcastReceiver();
寄存器接收器(m_接收器、滤波器);
Log.d(“widgetool”、“works”);
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
start();
Toast.makeText(getBaseContext(),“服务打开”,Toast.LENGTH\u SHORT)
.show();
返回开始时间;
}
@凌驾
公共空间{
停止();
未注册接收人(m_接收人);
}
@SuppressLint(“唤醒锁”)
公开作废开始(){
试一试{
pm=(PowerManager)getSystemService(POWER\u服务);
wl=pm.newWakeLock(PowerManager.SCREEN\u BRIGHT\u WAKE\u LOCK
|PowerManager.ACQUIRE_导致_唤醒,“.SensorActivity”);
wl.acquire();
KeyguardManager mgr=(KeyguardManager)getSystemService(Activity.KEYGUARD_服务);
KeyguardLock=经理newKeyguardLock(KEYGUARD\u服务);
lock.disableKeyguard();
//新选项卡ActivityPacs().service();
}捕获(例外e){
}
}
公共停车场(){
ActivityManager am=(ActivityManager)此
.getSystemService(活动和服务);
List taskInfo=am.getRunningTasks(1);
Log.d(“topActivity”,“当前活动:”
+taskInfo.get(0.topActivity.getClassName());
activityname=taskInfo.get(0.topActivity.getClassName();
ComponentName componentInfo=taskInfo.get(0).topActivity;
getPackageName();
}
私有类ScreenBroadcastReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文、意图){
if(intent.getAction().equals(intent.ACTION_屏幕_打开)){
Log.d(“在屏幕上”,“可能挂在这里”);
//start();
停止();
}else if(intent.getAction().equals(intent.ACTION\u SCREEN\u OFF)){
start();
Log.d(“屏蔽”、“可能挂在这里”);
}
}
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
}
在你的问题中。。。您必须在所有活动中使用广播接收器,然后只有您使用服务接收相应的信息。。

感谢您的回复。我想你都做了我想做的。实际上,我希望更多地分析这种方法有多好(在那些想要收听服务的活动中使用广播)以及应该采用什么方式。我的目的是根据服务内部发生的情况更新一些UI。
        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..