簡體   English   中英

讀取來電號碼

[英]Reading the incoming phone number

我是一名新的android開發人員。 我正在編寫一個應用程序,該應用程序獲取傳入的校准編號並執行一些操作。 我有一個活動“ Main_Activity”,Main_Activity中的代碼是這樣的:

    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);
    }



    }
    }

AndroidManifist.xml是這樣的:

    <?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>

我只想獲取該號碼並將其寫入LogCat,但是它不起作用,當我單擊應用程序圖標,上面寫着“不幸的是,呼叫檢查程序已停止”時,我看不到LogCat中的任何行。

確保清單中使用了以下權限

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM