Android的读取手机状态?状态、手机、Android

2023-09-12 03:44:27 作者:停止、想念

大家好一个初学者,我试图让应用程序,以读取手机状态键,当手机状态改变显示吐司的当前状态。有人可以送一个例子或帮助我在这里。

Hi guys I am a begginer and I am trying to make app to READ PHONE STATE and when the phone state is changed to display Toast with the current state. Can somebody send an example or help me here.

我的类:

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

我的清单是:

my manifest is :

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

我的布局是:

My layout is :

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

但是,当我启动它,应用程序意外终止?!?!?!谁能告诉我什么是错?

But when I start it,app stops unexpectedly?!?!?!Can somebody tell me what is wrong?

推荐答案

我没有看到&LT;使用-权限的Andr​​oid:名称=android.permission.READ_PHONE_STATE/&GT; 在清单文件。

I did not see <uses-permission android:name="android.permission.READ_PHONE_STATE" /> in your Manifest file.

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

It is required for your application to be able to read that state.