安卓读取电话状态?

17

我正在尝试制作一个应用程序,以便读取电话状态并在电话状态更改时显示Toast与当前状态。但是当我启动它时,应用程序意外停止。

我的类:

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.TextView;

public class TelephonyDemo extends Activity {
    TextView textOut;
    TelephonyManager telephonyManager;
    PhoneStateListener listener;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Get the UI
        textOut = (TextView) findViewById(R.id.textOut);

        // Get the telephony manager
        telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

        // Create a new PhoneStateListener
        listener = new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                String stateString = "N/A";
                switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    stateString = "Idle";
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    stateString = "Off Hook";
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    stateString = "Ringing";
                    break;
                }
                textOut.append(String.format("\nonCallStateChanged: %s",
                        stateString));
            }
        };

        // Register the listener with the telephony manager
        telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    }
}

我的清单是:

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

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Light" >
        <activity
            android:name=".TelephonyDemo"
            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>

    <uses-sdk android:minSdkVersion="7" />

</manifest>

我的布局是:

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Telephony Demo"
        android:textSize="22sp" />

    <TextView
        android:id="@+id/textOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Output" >
    </TextView>

</LinearLayout>

你需要发布一些代码或更详细地说明你想要做什么。我们不是来为你编写项目的。 - Codeman
可能会抛出SecurityException异常,因为您忘记在AndroidManifest.xml文件中添加READ_PHONE_STATE权限。 - Emmanuel Devaux
3个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
34

我在您的清单文件中没有看到<uses-permission android:name="android.permission.READ_PHONE_STATE" />

这是需要您的应用程序能够读取该状态的必要权限。


很棒的答案!你能告诉我如何让我的应用在手机开机时后台运行吗?! - AndBegginer
3
如果您的应用程序需要在后台运行,可以考虑使用Service而不是Activity:http://developer.android.com/reference/android/app/Service.html。我说"可以考虑"是因为我没有所有必要的上下文元素来确定这一点。 - Shlublu
2
要在手机开机时启动,请使用BOOT_COMPLETED广播接收器,并从其中启动您的服务。 - marcinj

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

    /* permission should be added like below*/
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Light" >
        <activity
            android:name=".TelephonyDemo"
            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>

    <uses-sdk android:minSdkVersion="7" />

</manifest>

1

你的应用程序通过由电信服务广播的Intent来了解电话状态,通知应用程序有关电话状态更改的情况。
您可能需要指南来创建您的应用程序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,