2

android調用和短信廣播接收器工作完美,直到棉花糖6.0,但在Android nougat,它是不工作時,應用程序關閉,當應用程序在後臺,那麼它在Android nougat(N)中工作得很好。請任何人都可以幫我解決這個問題。在mainfest文件添加安卓廣播接收器(通話和短信接收)不工作在Android nougat

public class CallReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { 
     savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER"); 
     Log.d("t savedNumber", savedNumber); 
     return; 
    }else{ 
     String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE); 
     String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 

     int state = 0; 
     if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){ 
      state = TelephonyManager.CALL_STATE_IDLE; 
     } 
     else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){ 
      state = TelephonyManager.CALL_STATE_OFFHOOK; 
     } 
     else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){ 
      state = TelephonyManager.CALL_STATE_RINGING; 
     } 

     Log.d("t incoming Number", number+" state: "+state+ " stateStr: "+stateStr); 

     onCallStateChanged(context, state, number); 
    } 
} 
} 

permisssion

<uses-permission android:name="android.permission.RECEIVE_SMS" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 

註冊接收

 <receiver android:name=".Receiver.CallReceiver "> 
     <intent-filter android:priority="100"> 
      <action android:name="android.intent.action.PHONE_STATE" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
     </intent-filter> 
    </receiver> 
+0

我還添加了此權限<使用權限android:name =「android.permission.CALL_PHONE」/><使用權限android:name =「android.permission.PROCESS_INCOMING_CALLS」/> –

+0

runTime權限丟失 – Eazyz

+0

@Eazyz我已經給出了運行時權限 –

回答

0

我想你忘了問了運行時間許可

開始在在Android 6.0(API等級23),用戶授予權限的應用程序,而應用程序正在運行,而不是當他們安裝的應用程序

使用下面的代碼添加運行權限READ_SMS:

String permission = Manifest.permission.READ_SMS; 
int grant = ContextCompat.checkSelfPermission(this, permission); 
if (grant != PackageManager.PERMISSION_GRANTED) { 
    String[] permission_list = new String[1]; 
    permission_list[0] = permission; 
    ActivityCompat.requestPermissions(this, permission_list, 1); 
} 

而不是手柄結果像這樣:

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, 
             @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if (requestCode == 1) { 
     if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      Toast.makeText(AccountClass.this,"permission granted", Toast.LENGTH_SHORT).show(); 
      // perform your action here 

     } else { 
      Toast.makeText(AccountClass.this,"permission not granted", Toast.LENGTH_SHORT).show(); 
     } 
    } 

} 

清單文件添加此權限:

<uses-permission android:name="android.permission.READ_SMS"/> 
+0

感謝您的快速回復。但我添加了運行時權限 –

+0

@DhananjayGupta,但你忘了添加這個權限''來讀取短信**你有權限接收短信,但不能閱讀短信** –

+0

我也加入了短信收到和閱讀短信許可,但我的問題是爲了接聽電話。它的作品時,應用程序是在後臺,但應用程序關閉,然後不工作的通話和短信廣播接收器。 –

3

ŧ ry這項服務..!

短信觀察

package com.demo.service; 

import android.content.Context; 
import android.content.Intent; 
import android.database.ContentObserver; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Handler; 
import android.util.Log; 
import android.widget.Toast; 


import org.json.JSONArray; 
import org.json.JSONObject; 

import java.util.HashMap; 
import java.util.Map; 


public class SmsObserver extends ContentObserver { 

    String TAG =SmsObserver.class.getSimpleName(); 

    static final Uri SMS_STATUS_URI = Uri.parse("content://sms"); 
    private Context mContext; 
    private String contactId = "", contactName = ""; 
    private String smsBodyStr = "", phoneNoStr = ""; 
    private long smsDatTime = System.currentTimeMillis(); 

    public SmsObserver(Handler handler, Context ctx) { 
     super(handler); 
     mContext = ctx; 
    } 

    public boolean deliverSelfNotifications() { 
     return true; 
    } 

    public void onChange(boolean selfChange) { 
     try { 
      Log.e("Info", "Notification on SMS observer"); 
      Cursor sms_sent_cursor = mContext.getContentResolver().query(SMS_STATUS_URI, null, null, null, null); 
      if (sms_sent_cursor != null) { 
       if (sms_sent_cursor.moveToFirst()) { 
        String protocol = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("protocol")); 
        Log.e("Info", "protocol : " + protocol); 
        //for send protocol is null 
        if (protocol == null) { 
         /* 
         String[] colNames = sms_sent_cursor.getColumnNames(); 
         if(colNames != null){ 
          for(int k=0; k<colNames.length; k++){ 
           Log.e("Info","colNames["+k+"] : " + colNames[k]); 
          } 
         } 
         */ 
         int type = sms_sent_cursor.getInt(sms_sent_cursor.getColumnIndex("type")); 
         Log.e("Info", "SMS Type : " + type); 
         // for actual state type=2 
         if (type == 2) { 
          Log.e("Info", "Id : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("_id"))); 
          Log.e("Info", "Thread Id : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("thread_id"))); 
          Log.e("Info", "Address : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address"))); 
          Log.e("Info", "Person : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("person"))); 
          Log.e("Info", "Date : " + sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date"))); 
          Log.e("Info", "Read : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("read"))); 
          Log.e("Info", "Status : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("status"))); 
          Log.e("Info", "Type : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("type"))); 
          Log.e("Info", "Rep Path Present : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("reply_path_present"))); 
          Log.e("Info", "Subject : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("subject"))); 
          Log.e("Info", "Body : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body"))); 
          Log.e("Info", "Err Code : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("error_code"))); 

          smsBodyStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body")).trim(); 
          phoneNoStr = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address")).trim(); 
          smsDatTime = sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date")); 

          Log.e("Info", "SMS Content : " + smsBodyStr); 
          Log.e("Info", "SMS Phone No : " + phoneNoStr); 
          Log.e("Info", "SMS Time : " + smsDatTime); 

         } 
        } 
       } 
      } else 
       Log.e("Info", "Send Cursor is Empty"); 
     } catch (Exception sggh) { 
      Log.e("Error", "Error on onChange : " + sggh.toString()); 
     } 
     super.onChange(selfChange); 
    }//fn onChange 


}//End of class SmsObserver 

手機狀態服務

package com.demo.service; 

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Binder; 
import android.os.IBinder; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.util.Log; 
import android.widget.Toast; 


import org.json.JSONArray; 
import org.json.JSONObject; 

import java.lang.reflect.Method; 
import java.util.HashMap; 
import java.util.Map; 


public class PhoneStateService extends Service { 

    String TAG = PhoneStateService.class.getSimpleName(); 

    boolean isMissedCall = false; 
    boolean isRingingCall = false; 
    boolean isRingingCallOne = false; 

    String onOff; 
    String callerID; 
    String callerIDOne; 
    String[] cIds; 
    String[] cIdsOne; 

    private CallStateListener mCallStateListener = new CallStateListener(); 
    private TelephonyManager mTelephonyManager; 
    private int mCallState; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mTelephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); 
     mCallState = mTelephonyManager.getCallState(); 
     mTelephonyManager.listen(mCallStateListener, PhoneStateListener.LISTEN_CALL_STATE); 
    } 

    @Override 
    public void onDestroy() { 
     Log.d("onDestroy", "onDestroy"); 
     mTelephonyManager.listen(mCallStateListener, PhoneStateListener.LISTEN_NONE); 
     super.onDestroy(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; //-- not a bound service-- 
    } 

    private final class CallStateListener extends PhoneStateListener { 
     @Override 
     public void onCallStateChanged(int state, String incomingNumber) { 

      String getSimSerialNumber = mTelephonyManager.getSimSerialNumber(); 
      String mPhoneNumber = mTelephonyManager.getLine1Number(); 


      Log.d("number", "getSimSerialNumber : " + getSimSerialNumber); 
      Log.d("number", "mPhoneNumber : " + mPhoneNumber); 

      Log.d("number", "number : " + incomingNumber+" callstatelistner : "+state + " "+Consts.number); 

      onOff = SharedPreferenceUtil.getString("onOff",""); 

      callerID = SharedPreferenceUtil.getString("callerID",""); 

      callerIDOne = SharedPreferenceUtil.getString("callerIDOne",""); 

      cIds = new String[0]; 

      cIdsOne = new String[0]; 
      Log.d(TAG,"callerID : "+ callerID); 
      Log.d(TAG,"callerIDOne : "+ callerIDOne); 
      if (!callerID.equals("")) { 
       try { 
        callerID = callerID.substring(0, callerID.length() - 1); 
        cIds = callerID.split(","); 
        Log.d(TAG,"callerID :: "+ cIds.toString()); 
        Log.d(TAG,"callerID :: "+ cIds.length); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
      if (!callerIDOne.equals("")) { 
       try { 
        callerIDOne = callerIDOne.substring(0, callerIDOne.length() - 1); 
        cIdsOne = callerIDOne.split(","); 

        Log.d(TAG,"callerIDOne :: "+ cIdsOne.toString()); 
        Log.d(TAG,"callerIDOne :: "+ cIdsOne.length); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 

      int OFFHOOK = TelephonyManager.CALL_STATE_OFFHOOK; 
      int IDLE = TelephonyManager.CALL_STATE_IDLE; 
      int RINGING = TelephonyManager.CALL_STATE_RINGING; 

// my change 
      mCallState = state; 
//   Log.v("state","-- "+state+ " --- "+TelephonyManager.CALL_STATE_IDLE+" - "+TelephonyManager.CALL_STATE_OFFHOOK+" - "+TelephonyManager.CALL_STATE_RINGING); 
      Log.v("state","-- "+state+ " --- "+IDLE+" - "+OFFHOOK+" - "+RINGING); 
      if (state == 0){ 
       Log.d(TAG, "state ....0"); 
       if (state == OFFHOOK) { 
        Log.d("state", "idle --> off hook = new outgoing call"+ Consts.number); 
        // idle --> off hook = new outgoing call 
        //triggerSenses(Sense.CallEvent.OUTGOING); 

        callStateOffhoof(incomingNumber); 
       } else if (state == RINGING) { 
        Log.d("state", "idle --> ringing = new incoming call"+Consts.number); 
        // idle --> ringing = new incoming call 
        //triggerSenses(Sense.CallEvent.INCOMING); 
        callStateRinging(incomingNumber); 
       }else if (state == IDLE) { 
        Log.d(TAG, "state ....IDLE"); 
        Log.d("state", "ringing --> idle = missed call"+Consts.number); 
        // ringing --> idle = missed call 
        //triggerSenses(Sense.CallEvent.MISSED); 
        callStateIdeal(incomingNumber); 
       } 
      }else if (state == 1){ 
       Log.d(TAG, "state ....1"); 
       if (state == OFFHOOK) { 
        Log.d("state", "ringing --> off hook = received"+Consts.number); 
        // ringing --> off hook = received 
        //triggerSenses(Sense.CallEvent.RECEIVED); 
        callStateOffhoof(incomingNumber); 
       } else if (state == IDLE) { 
        Log.d("state", "ringing --> idle = missed call"+Consts.number); 
        // ringing --> idle = missed call 
        //triggerSenses(Sense.CallEvent.MISSED); 
        callStateIdeal(incomingNumber); 
       }else if (state == RINGING) { 
        Log.d("state", "idle --> ringing = new incoming call"+Consts.number); 
        // idle --> ringing = new incoming call 
        //triggerSenses(Sense.CallEvent.INCOMING); 
        callStateRinging(incomingNumber); 
       } 
      }else if (state == 2){ 
       Log.d(TAG, "state ....2"); 
       if (state == IDLE) { 
        Log.d("state", "off hook --> idle = disconnected"+Consts.number); 
        // off hook --> idle = disconnected 
        //triggerSenses(Sense.CallEvent.ENDED); 
        callStateIdeal(incomingNumber); 
       } else if (state == RINGING) { 
        Log.d("state", "off hook --> ringing = another call waiting"+Consts.number); 
        // off hook --> ringing = another call waiting 
        //triggerSenses(Sense.CallEvent.WAITING); 
        callStateRinging(incomingNumber); 
       }else if(state==OFFHOOK) 
       { 
        String nm = Consts.number; 
        if (!nm.isEmpty()){ 
         callStateOffhoof(nm); 
        }else { 
         callStateOffhoof(incomingNumber); 
        } 

       } 
       Log.d("CALL_STATE_OFFHOOK", String.valueOf(state)); 
      } 
      /*switch (state) { 
       case 0: 
        if (state == OFFHOOK) { 
         Log.d("state", "idle --> off hook = new outgoing call"+ Consts.number); 
         // idle --> off hook = new outgoing call 
         //triggerSenses(Sense.CallEvent.OUTGOING); 

         callStateOffhoof(incomingNumber); 
        } else if (state == RINGING) { 
         Log.d("state", "idle --> ringing = new incoming call"+Consts.number); 
         // idle --> ringing = new incoming call 
         //triggerSenses(Sense.CallEvent.INCOMING); 
         callStateRinging(incomingNumber); 
        } 

        break; 

       case 2: 
        if (state == IDLE) { 
         Log.d("state", "off hook --> idle = disconnected"+Consts.number); 
         // off hook --> idle = disconnected 
         //triggerSenses(Sense.CallEvent.ENDED); 
         callStateIdeal(incomingNumber); 
        } else if (state == RINGING) { 
         Log.d("state", "off hook --> ringing = another call waiting"+Consts.number); 
         // off hook --> ringing = another call waiting 
         //triggerSenses(Sense.CallEvent.WAITING); 
         callStateRinging(incomingNumber); 
        } 
        else if(state==OFFHOOK) 
        { 
         String nm = Consts.number; 
         if (!nm.isEmpty()){ 
          callStateOffhoof(nm); 
         }else { 
          callStateOffhoof(incomingNumber); 
         } 

        } 
        Log.d("CALL_STATE_OFFHOOK", String.valueOf(state)); 
        break; 

       case 1: 
        if (state == OFFHOOK) { 
         Log.d("state", "ringing --> off hook = received"+Consts.number); 
         // ringing --> off hook = received 
         //triggerSenses(Sense.CallEvent.RECEIVED); 
         callStateOffhoof(incomingNumber); 
        } else if (state == IDLE) { 
         Log.d("state", "ringing --> idle = missed call"+Consts.number); 
         // ringing --> idle = missed call 
         //triggerSenses(Sense.CallEvent.MISSED); 
         callStateIdeal(incomingNumber); 
        } 
        break; 
      }*/ 
      Log.d(TAG, "Outer state....."); 
//   mCallState = state; change 
     } 
    } 

    public static void init(Context c) { 
     c.startService(new Intent(c, PhoneStateService.class)); 
     Log.d("Service enabled","Service enabled: " + true); 
    } 
    boolean b; 
    public void callStateRinging(String incomingNumber){ 
     /*Toast.makeText(getApplicationContext(), "Phone Is Riging", 
       Toast.LENGTH_LONG).show();*/ 
     isMissedCall = true; 
     isRingingCall = true; 
     isRingingCallOne = true; 

     Log.d(TAG,"Rebound : "+ onOff); 
     if (onOff.equals("on")){ 

      b = false; 
      for (int i=0; i<cIds.length; i++) { 
       if (cIds[i].equals(incomingNumber)){ 
        b = true; 
       }else if (incomingNumber.contains(cIds[i])){ 
        b = true; 
       } 
      } 
      if (!b){ 
       for (int i=0; i<cIdsOne.length; i++) { 
        if (cIdsOne[i].equals(incomingNumber)){ 
         b = true; 
        }else if (incomingNumber.contains(cIdsOne[i])){ 
         b = true; 
        } 
       } 
      } 
      Log.d(TAG,"b : "+b); 
      if (!b) { 
       Log.d(TAG,"Rebound : disconnectCall"); 
       disconnectCall(); 
      }else { 
       Log.d(TAG,"Rebound : not disconnectCall because it allow contact..."); 
      } 
     }else if (onOff.equals("off")){ 
      Log.d(TAG,"Rebound Off"); 
     } 
     Log.d(TAG,"callStateRinging Rebound outer : "+ onOff); 
     Log.d(TAG,"callStateRinging Rebound cIds : "+ cIds.length); 
     Log.d(TAG,"callStateRinging Rebound callerID: "+ callerID); 
     Log.d("number", "callStateRinging number : " + incomingNumber); 
    } 

    public void callStateIdeal(String incomingNumber){ 
     /*Toast.makeText(getApplicationContext(), "phone is neither ringing nor in a call", 
       Toast.LENGTH_LONG).show();*/ 

     if (!isRingingCall){ 
         if (isRingingCallOne) { 
          Intent service; 
//       service = new Intent(this, WaitingMessageService.class); 
//       startService(service); 
          Log.d(TAG, "Servic called"); 
//       new Handler().postDelayed(stopWaitnigPopUp, 10000); 
         } 
      Log.d(TAG,"Rebound in A call : "+ onOff); 
      if (Consts.isInternet()) { 
//       if (onOff.equals("on")) { 

       String nm = Consts.number; 
       String onlyDigitNum = Consts.getOnlyDigits(nm); 
       String onlyDigitNumOne = Consts.getOnlyDigits(incomingNumber); 
       Log.d(TAG,"nm : "+ nm); 
       Log.d(TAG,"incomingNumber : "+ incomingNumber); 
       Log.d(TAG,"onlyDigitNum : "+ onlyDigitNum); 
       Log.d(TAG,"onlyDigitNumOne : "+ onlyDigitNumOne); 

//       } 
      }else { 
       Log.d(TAG,"No Internet so not send any rebounds"); 
      } 
     } 

     if (isMissedCall){ 
      Log.d(TAG,"Rebound : "+ onOff); 

      if (Consts.isInternet()) { 
       if (onOff.equals("on")) { 
        isMissedCall = false; 

       } 
      }else { 
       Log.d(TAG,"No Internet so not send any rebounds"); 
      } 

         /*int count = dbHelper.numberOfRows(); 
         Log.d(TAG,"count :" + count); 
         if (count != 0){ 
          Cursor cursor = dbHelper.getAllData(); 
          if (cursor.moveToFirst()) { 
           for (int j = 0; j < count; j++) { 
            String alive = cursor.getString(1); 
            String cantactno = cursor.getString(2); 
            String multimidiyatype = cursor.getString(3); 
            String path = cursor.getString(4); 
            String credate = cursor.getString(5); 
            String message = cursor.getString(6); 

            if (cantactno.equals(incomingNumber)){ 
             Toast.makeText(getApplicationContext(),"Message "+ message + " path :"+ path +" Contact NO;"+ cantactno,Toast.LENGTH_SHORT).show(); 
            } 

            Log.d(TAG,"--"+incomingNumber + cantactno + path + message); 

            cursor.moveToNext(); 
           } 
          } 
         }*/ 
     } 
     Log.d(TAG,"callStateIdeal Rebound outer : "+ onOff); 
     Log.d(TAG,"callStateIdeal Rebound cIds : "+ cIds.length); 
     Log.d(TAG,"callStateIdeal Rebound callerID: "+ callerID); 
     Log.d("number", "callStateIdeal number : " + incomingNumber); 
    } 

    public void callStateOffhoof(String incomingNumber){ 
     /*Toast.makeText(getApplicationContext(), "Phone is Currently in A call", 
       Toast.LENGTH_LONG).show();*/ 
     isMissedCall = false; 
     isRingingCall = false; 
     isRingingCallOne = true; 

     if (!isRingingCall){ 
         Intent service; 
//      service = new Intent(this,WaitingMessageService.class); 
//      startService(service); 
         Log.d(TAG,"Servic called"); 
//      new Handler().postDelayed(stopWaitnigPopUp, 10000); 

      Log.d(TAG,"Rebound in A call : "+ onOff); 
      if (Consts.isInternet()) { 
//       if (onOff.equals("on")) { 

       String nm = Consts.number; 
       String onlyDigitNum = Consts.getOnlyDigits(nm); 
       String onlyDigitNumOne = Consts.getOnlyDigits(incomingNumber); 
       Log.d(TAG,"nm : "+ nm); 
       Log.d(TAG,"incomingNumber : "+ incomingNumber); 
       Log.d(TAG,"onlyDigitNum : "+ onlyDigitNum); 
       Log.d(TAG,"onlyDigitNumOne : "+ onlyDigitNumOne); 

//       } 
      }else { 
       Log.d(TAG,"No Internet so not send any rebounds"); 
      } 
     } 
     Log.d(TAG,"callStateOffhoof Rebound outer : "+ onOff); 
     Log.d(TAG,"callStateOffhoof Rebound cIds : "+ cIds.length); 
     Log.d(TAG,"callStateOffhoof Rebound callerID: "+ callerID); 
     Log.d("number", "callStateOffhoof number : " + incomingNumber); 
    } 

    public void disconnectCall(){ 
     try { 
      String serviceManagerName = "android.os.ServiceManager"; 
      String serviceManagerNativeName = "android.os.ServiceManagerNative"; 
      String telephonyName = "com.android.internal.telephony.ITelephony"; 
      Class<?> telephonyClass; 
      Class<?> telephonyStubClass; 
      Class<?> serviceManagerClass; 
      Class<?> serviceManagerNativeClass; 
      Method telephonyEndCall; 
      Object telephonyObject; 
      Object serviceManagerObject; 
      telephonyClass = Class.forName(telephonyName); 
      telephonyStubClass = telephonyClass.getClasses()[0]; 
      serviceManagerClass = Class.forName(serviceManagerName); 
      serviceManagerNativeClass = Class.forName(serviceManagerNativeName); 
      Method getService = // getDefaults[29]; 
        serviceManagerClass.getMethod("getService", String.class); 
      Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class); 
      Binder tmpBinder = new Binder(); 
      tmpBinder.attachInterface(null, "fake"); 
      serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder); 
      IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone"); 
      Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class); 
      telephonyObject = serviceMethod.invoke(null, retbinder); 
      telephonyEndCall = telephonyClass.getMethod("endCall"); 
      telephonyEndCall.invoke(telephonyObject); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      /*Log.error(DialerActivity.this, 
        "FATAL ERROR: could not connect to telephony subsystem"); 
      Log.error(DialerActivity.this, "Exception object: " + e);*/ 
     } 
    } 


} 

我希望它爲你工作..!

+0

感謝您的重播,但是當應用程序從最近的應用程序中死亡,然後在android 7.0中不工作廣播接收器。 –