Android showing device Call-Log screen after call gets finished/End [duplicate]

余生颓废 提交于 2019-12-04 18:50:20

yesterday i made a simple app , it makes call and after ending the call come back to Base Activity

here is my code may help you

MakePhoneCallActivity.java

package com.rdc;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MakePhoneCallActivity extends Activity { 
    private Button button; 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        button = (Button) findViewById(R.id.buttonCall); 
        // add button listener
        button.setOnClickListener(new OnClickListener() { 
            @Override
            public void onClick(View arg0) {

                Intent callIntent = new Intent(Intent.ACTION_CALL);
                callIntent.setData(Uri.parse("tel:9741817902"));
                startActivity(callIntent); 
            } 
        });         
        // add PhoneStateListener
        PhoneCallListener phoneListener = new PhoneCallListener();
        TelephonyManager telephonyManager = (TelephonyManager) this
                    .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneListener,
                PhoneStateListener.LISTEN_CALL_STATE);
    }

    //monitor phone call activities
    private class PhoneCallListener extends PhoneStateListener {     
        private boolean isPhoneCalling = false;  
        String TAG = "DEBUG";    
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if (TelephonyManager.CALL_STATE_RINGING == state) {
                    // phone ringing
                Log.i(TAG, "RINGING, number: " + incomingNumber);
            }    
            if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                    // active
                Log.i(TAG, "OFFHOOK");   
                isPhoneCalling = true;
            }    
            if (TelephonyManager.CALL_STATE_IDLE == state) {
                // run when class initial and phone call ended, 
                // need detect flag from CALL_STATE_OFFHOOK
                Log.i(TAG, "IDLE");  
                if (isPhoneCalling) {

                    Log.i(TAG, "restart app");   
                    // restart app
                    Intent i = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(
                            getBaseContext().getPackageName());
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);    
                    isPhoneCalling = false;
                }    
            }
        }
    }   
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/buttonCall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Make a call" />

</LinearLayout>

and my manifest is

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.rdc"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

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

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MakePhoneCallActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

let me know if still any trouble is there.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!