Android读取手机状态?
我正在尝试使应用程序成为Android读取手机状态?,android,android-manifest,Android,Android Manifest,我正在尝试使应用程序成为读取手机状态,当手机状态更改为显示当前状态的Toast。但当我启动它时,应用程序意外停止 我的班级: import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import andro
读取手机状态
,当手机状态更改为显示当前状态的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>
您的应用程序知道电话状态,这要感谢电话服务广播的意图,该意图通知应用程序电话状态的更改。
您可能需要创建应用程序的指南
- 意向:有关详细信息,请参阅,有关概念和电话管理器,请参阅。ACTION_PHONE_STATE_CHANGED是您需要接收感谢您的BoradCastereceiver的意向的名称
- 广播接收机,请参阅“应用程序组件”部分
- android.permission.READ_PHONE_STATE权限必须添加到android manifest.xml文件中(这里是一个示例摘录..)
...
您的应用程序必须能够读取该状态。
<?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>
/*应该像下面那样添加权限*/
您需要发布一些代码或更详细地说明您要做的事情。我们不是来为您编写项目的。可能是因为您忘记在AndroidManifest.xml文件中添加READ_PHONE_STATE权限而引发了SecurityException太棒了!你能告诉我,当手机开机时,我如何让我的应用程序在后台运行吗?!如果你的应用程序应该在后台运行,你可能愿意做一项服务而不是一项活动:-我说可能是因为我没有确定这一点所需的所有上下文元素。要启动手机电源,请使用BOOT_完成的广播接收器,并从中启动你的服务。
<?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>