SERVICE可以自己发广播消息,也可以提供接口(IBINDER机制)被ACTIVITY调用,但不知道它自己能不能接手广播消息而不要ACTIVITY的参与。
解决方案 »
- 如何绑定Listview和checkbox?
- 如何监测对方手机终端有无QQ
- android 本地html无法播放mp3
- 导入别人写的项目作为lib出不明错误
- 纠结好几年了!这个错到底怎么改???
- 非常纠结bitmap的问题,困扰了好久
- 通过花生壳域名访问内网问题=====================>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>急!急!急!
- 有段UDP SOCKET的代码,不知道怎么添加到线程中去,请高手帮一下
- 求助百度地图怎么用啊 Android 小白啊
- 還是搞不懂 import static 與 import 有何不同之處!
- 测试一个小控件总提示force close?
- 请教 intent filter 的问题
我看到很多例子都是不行的啊!都是有ACTIVITY的参与。如果可以在工程的XML文件中RECIVER指定给谁?ACTIVITY吗?那不还是由ACTIVITY接收的?
你这个是不是要求PhoneStatReceiver类为MusicService类的内部类?
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
http://www.cnblogs.com/tt_mc/archive/2010/03/03/1677474.html
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);
}
}
}
}