这是AddressService.java: 我不知道为什么我不能收到电话的播报。 我已经在AndroidManifest中注册了它,但它无法正常工作' 不知道为什么,我不能运行程序, 我找不到任何错误。
package com.example.mobilesafe.service;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import com.example.mobilesafe.R;
import com.example.mobilesafe.db.dao.NumberAddressQueryUtils;
public class AddressService extends Service {
/**
* 窗体管理者
*/
private WindowManager wm;
private View view;
/**
* 电话服务
*/
private TelephonyManager tm;
private MyListenerPhone listenerPhone;
private OutCallReceiver receiver;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
// 服务里面的内部类
//广播接收者的生命周期和服务一样
class OutCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// 这就是我们拿到的播出去的电话号码
String phone = getResultData();
// 查询数据库
String address = NumberAddressQueryUtils.queryNumberAddress(phone);
// Toast.makeText(context, address, 1).show();
myToast(address);
}
}
private class MyListenerPhone extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// state:状态,incomingNumber:来电号码
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:// 来电铃声响起
// 查询数据库的操作
String address = NumberAddressQueryUtils
.queryNumberAddress(incomingNumber);
// Toast.makeText(getApplicationContext(), address, 1).show();
myToast(address);
break;
case TelephonyManager.CALL_STATE_IDLE://电话的空闲状态:挂电话、来电拒绝
//把这个View移除
if(view != null ){
wm.removeView(view);
}
break;
default:
break;
}
}
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
// 监听来电
listenerPhone = new MyListenerPhone();
tm.listen(listenerPhone, PhoneStateListener.LISTEN_CALL_STATE);
//用代码去注册广播接收者
receiver = new OutCallReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.NEW_OUTGOING_CALL");
registerReceiver(receiver, filter);
//实例化窗体
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
}
/**
* 自定义土司
* @param address
*/
public void myToast(String address) {
view = View.inflate(this, R.layout.address_show, null);
TextView textview = (TextView) view.findViewById(R.id.tv_address);
//"半透明","活力橙","卫士蓝","金属灰","苹果绿"
int [] ids = {R.drawable.call_locate_white,R.drawable.call_locate_orange,R.drawable.call_locate_blue
,R.drawable.call_locate_gray,R.drawable.call_locate_green};
SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
view.setBackgroundResource(ids[sp.getInt("which", 0)]);
textview.setText(address);
//窗体的参数就设置好了
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
wm.addView(view, params);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
// 取消监听来电
tm.listen(listenerPhone, PhoneStateListener.LISTEN_NONE);
listenerPhone = null;
//用代码取消注册广播接收者
unregisterReceiver(receiver);
receiver = null;
}
}
AndroidMainfest.xml的部分如下: 我已在其中添加了aciton。
<service android:name="com.example.mobilesafe.service.AddressService">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</service>
答案 0 :(得分:0)
slm;我认为您必须在清单中添加权限:
机器人:名称=&#34; android.permission.READ_PHONE_STATE&#34; /&GT;并且:
机器人:名称=&#34; android.permission.PROCESS_OUTGOING_CALLS&#34; /&GT;