我是一个新的安卓开发者。我正在编写一个应用程序来获取传入的呼叫号码并做一些事情。我有一个活动“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 中的任何行,它说“不幸的是,呼叫检查器已停止”