简体   繁体   中英

Reading the incoming phone number

i am a new android developer . i am writing a application that get the incoming caling number and do some stuff. i have one Activity " Main_Activity " the code in Main_Activity is this :

    package com.example.callchecker;

    import android.os.Bundle;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;

    import android.telephony.TelephonyManager;
    import android.util.Log;




    public class Main_Activity extends BroadcastReceiver{public Main_Activity() {
// TODO Auto-generated constructor stub
    }
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    try {
        Bundle extras=intent.getExtras();
        if (extras !=null){

            String state = extras.getString(TelephonyManager.EXTRA_STATE);
            Log.w("MY_DEBUG_TAG",state);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                String phonenumber = extras.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                Log.w("MY_DEBUG_TAG", phonenumber);

            }
        }


    } catch (Exception e) {
        // TODO: handle exception
        Log.w("MY_DEBUG_TAG", e);
    }



    }
    }

and the AndroidManifist.xml is this :

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

        <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />


        <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.callchecker.Main_Activity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name="Main_Activity" >
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE"/>

        </intent-filter>

    </receiver>
    </application>

    </manifest>

I just want to get the number and write it to LogCat But it is not working , i can not see any line in LogCat when i click on app icon it say " Unfortunately call checker has been stopped "

Make sure the following permission is used in manifest

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM