How to detect incoming calls, in an Android device?

前端 未结 10 1706
一向
一向 2020-11-22 02:27

I\'m trying to make an app like, when a call comes to the phone I want to detect the number. Below is what I tried, but it\'s not detecting incoming calls.

I want t

10条回答
  •  醉梦人生
    2020-11-22 02:37

    Please use the below code. It will help you to get the incoming number with other call details.

    activity_main.xml

    
    
    
    
    
    

    MainActivity.java

    public class MainActivity extends Activity {
    
    private static final int MISSED_CALL_TYPE = 0;
    private TextView txtcall;
    
    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        txtcall = (TextView) findViewById(R.id.call);
    
        StringBuffer sb = new StringBuffer();
        Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
                null, null, null);
        int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
        int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
        int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
        int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
        sb.append("Call Details :");
        while (managedCursor.moveToNext()) {
            String phNumber = managedCursor.getString(number);
            String callType = managedCursor.getString(type);
            String callDate = managedCursor.getString(date);
            Date callDayTime = new Date(Long.valueOf(callDate));
            String callDuration = managedCursor.getString(duration);
            String dir = null;
            int dircode = Integer.parseInt(callType);
            switch (dircode) {
    
            case CallLog.Calls.OUTGOING_TYPE:
                dir = "OUTGOING";
                break;
    
            case CallLog.Calls.INCOMING_TYPE:
                dir = "INCOMING";
                break;
    
            case CallLog.Calls.MISSED_TYPE:
                dir = "MISSED";
                break;
            }
            sb.append("\nPhone Number:--- " + phNumber + " \nCall Type:--- "
                    + dir + " \nCall Date:--- " + callDayTime
                    + " \nCall duration in sec :--- " + callDuration);
            sb.append("\n----------------------------------");
        }
        managedCursor.close();
        txtcall.setText(sb);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    } 
    

    and in your manifest request for following permissions:

    
    
    

提交回复
热议问题