android 监听短信和来电

 

 

权限

 
 
 
 

清单文件注册

   
        
            
                
                
            
        

 

启动服务

 Intent serviceIntent = new Intent(mActivity,SmsAndPhoneService.class);
  mActivity.stopService(serviceIntent );

 

  具体代码


import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsMessage;
import android.text.TextUtils;
import android.util.Log;

import com.dapp.yilian.deviceManager.DeviceConstant;
import com.dapp.yilian.mvp.entity.MyApplication;
import com.dapp.yilian.utils.LogUtils;
import com.sxr.sdk.ble.keepfit.service.BluetoothLeService;


public class SmsAndPhoneService extends Service {
    String TAG = "SmsAndPhoneService";

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.e(TAG, "----------------onStartCommand--------->");
        IntentFilter localIntentFilter = new IntentFilter();
        localIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        localIntentFilter.addAction("android.intent.action.PHONE_STATE");
        registerReceiver(this.NLServerReceiver, localIntentFilter);

    }


    public void onDestroy() {
        Log.d(this.TAG, "onDestroy");
        unregisterReceiver(this.NLServerReceiver);
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int i, int i1) {
        LogUtils.e(TAG, "----------------onStartCommand--------->");
        try {
            unregisterReceiver(this.NLServerReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            IntentFilter localIntentFilter = new IntentFilter();
            localIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
            localIntentFilter.addAction("android.intent.action.PHONE_STATE");
            registerReceiver(this.NLServerReceiver, localIntentFilter);
            LogUtils.e(TAG, "----------------registerReceiver--------->"+NLServerReceiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.onStartCommand(intent, i, i1);
    }

    private BroadcastReceiver NLServerReceiver = new BroadcastReceiver() {
        public void onReceive(Context paramContext, Intent paramIntent) {
            String action = paramIntent.getAction();
            if (action == null)
                return;
            if (action.equals("android.provider.Telephony.SMS_RECEIVED")) {
                String str = "";
                //---接收传入的消息---
                Bundle bundle = paramIntent.getExtras();
                SmsMessage[] msgs = null;
                if (bundle != null) {
                    //---查询到达的消息---
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];
                    for (int i = 0; i < msgs.length; i++) {
                        msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        if (i == 0) {
                            //---获取发送者手机号---
                            str += msgs[i].getOriginatingAddress();
                            str += ":";
                        }
                        //---获取消息内容---
                        str += msgs[i].getMessageBody().toString();
                    }
                    //---显示SMS消息---
                    LogUtils.e(TAG, str);
                    try {
                  
                            callNotify("1", "", "您有一条新的短消息,请注意查收!");
                        
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            if (action.equals("android.intent.action.PHONE_STATE")) {
                String state = paramIntent.getStringExtra("state");
                String incoming_number = paramIntent.getStringExtra("incoming_number");
                LogUtils.e(TAG, "state==" + state + "----------incoming_number==" + incoming_number);

                if ("RINGING".equals(state)) {
                    if (!TextUtils.isEmpty(incoming_number)) {
                        String phoneNumber = "";        // 剔除号码中的分隔符
                        String name = "";
                        try {
                            phoneNumber = incoming_number.replace("-", "").replace(" ", "");
                            name = PhoneUtils.getContactName(paramContext, phoneNumber);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        try {
                       
                                callNotify("0", name, phoneNumber);
                           
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }

                if ("IDLE".equals(state)) {//挂断

                }

                return;
            }

        }
    };


    private void callNotify(String type, String name, String content) {
        Intent counterIntent = new Intent(NLReceiver.ACTION_NOTIFY);//显示指定组件名
        counterIntent.putExtra("type", type);
        counterIntent.putExtra("name", name);
        counterIntent.putExtra("content", content);
//        //ComponentName("当前类的包名","包名.这条广播的接收器的类名")
        counterIntent.setComponent(new ComponentName(NLReceiver.packageName, 
        NLReceiver.className));//显示指定组件名称
        sendBroadcast(counterIntent);
    }

}

 

你可能感兴趣的:(Android)