SERVICE可以自己发广播消息,也可以提供接口(IBINDER机制)被ACTIVITY调用,但不知道它自己能不能接手广播消息而不要ACTIVITY的参与。

解决方案 »

  1.   

    肯定是可以的,需要做个BroadcastReceiver
      

  2.   


    我看到很多例子都是不行的啊!都是有ACTIVITY的参与。如果可以在工程的XML文件中RECIVER指定给谁?ACTIVITY吗?那不还是由ACTIVITY接收的?
      

  3.   


    你这个是不是要求PhoneStatReceiver类为MusicService类的内部类?
      

  4.   

    是内部类,从.MusicService$PhoneStatReceiver就可以看出来。
      

  5.   

    试了一下不行啊!!!E/AndroidRuntime( 1216): java.lang.RuntimeException: Unable to instantiate receiver com.KT.MAP100_MainService.MainService$myBroadcastReceiver: java.lang.InstantiationException: com.KT.MAP100_MainService.MainService$myBroadcastReceiver
    E/AndroidRuntime( 1216):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2520)
    E/AndroidRuntime( 1216):  at android.app.ActivityThread.access$3000(ActivityThread.java:116)
    E/AndroidRuntime( 1216):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1843)
    E/AndroidRuntime( 1216):  at android.os.Handler.dispatchMessage(Handler.java:99)
    E/AndroidRuntime( 1216):  at android.os.Looper.loop(Looper.java:123)
    E/AndroidRuntime( 1216):  at android.app.ActivityThread.main(ActivityThread.java:4203)
    E/AndroidRuntime( 1216):  at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime( 1216):  at java.lang.reflect.Method.invoke(Method.java:521)
    E/AndroidRuntime( 1216):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
    E/AndroidRuntime( 1216):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
    E/AndroidRuntime( 1216):  at dalvik.system.NativeStart.main(Native Method)
    E/AndroidRuntime( 1216): Caused by: java.lang.InstantiationException: com.KT.MAP100_MainService.MainService$myBroadcastReceiver
    E/AndroidRuntime( 1216):  at java.lang.Class.newInstanceImpl(Native Method)
    E/AndroidRuntime( 1216):  at java.lang.Class.newInstance(Class.java:1472)
    E/AndroidRuntime( 1216):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2513)
    E/AndroidRuntime( 1216):  ... 10 more
      

  6.   

    不知道是不是因为在XML文件里面的放的分支不对还是什么其它。
      

  7.   

    Android下实现程序关闭后,监听取消 
    http://www.cnblogs.com/tt_mc/archive/2010/03/03/1677474.html
      

  8.   

    如下用语句注册才是可以的package com.demo;
    import android.app.Service;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.telephony.gsm.SmsMessage;public class ListenService extends Service {
    public static final String SvrId = "RARNU.SERVICE.DEMO";
    public static final String strACT = "android.provider.Telephony.SMS_RECEIVED";
    private SMSMsgReceiver recv;
    @Override
    public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
    }
    @Override
    public void onCreate() {
    IntentFilter filter = new IntentFilter(strACT);
    recv = new SMSMsgReceiver();
    registerReceiver(recv, filter);
    super.onCreate();
    }
    @Override
    public IBinder onBind(Intent intent) {
    return null;
    }
    @Override
    public void onDestroy() {
    unregisterReceiver(recv);
    super.onDestroy();
    }
    public class SMSMsgReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(strACT)) {
    StringBuilder sb = new StringBuilder();
    Bundle bundle = intent.getExtras();
    if (bundle != null) {
    Object[] pdus = (Object[]) bundle.get("pdus");
    SmsMessage[] msg = new SmsMessage[pdus.length];
    for (int i = 0; i < pdus.length; i++) {
    msg[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
    }
    for (SmsMessage currMsg : msg) {
    sb.append("From:");
    sb.append(currMsg.getDisplayOriginatingAddress());
    sb.append("\nMessage:");
    sb.append(currMsg.getDisplayMessageBody());
    }
    }
    Intent i = new Intent(SvrId);
    i.putExtra("PARAM", sb.toString());
    sendBroadcast(i);
    }
    }
    }
    }