广播接收器不扣除电话 - Android版 [英] Broadcast Receiver does not deduct calls - Android
问题描述
我试图做的来电时间做一些工作。我想这样的
I am trying to do some work at incoming call times. I am trying like this
public class callDeductor extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
Toast.makeText(context, "Phone Is Ringing", Toast.LENGTH_LONG).show();
}
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
Toast.makeText(context, "Call Recieved", Toast.LENGTH_LONG).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
Toast.makeText(context, "Phone Is Idle", Toast.LENGTH_LONG).show();
}
}
}
这是我的Manifeast:
This is my Manifeast:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.deduct.calldeduct"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="Call Deduct" >
<receiver android:name="com.deduct.calldeduct.callDeductor" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
</manifest>
我检查与移动和仿真器。但它并没有显示出任何吐司消息。请让我知道我做了错误的。
I check with mobile and emulator. But it does not show any toast message. Please let me know where I did mistake.
我从这个网站的引用。
链接1
<一href=\"http://androidlabs.org/short-experiments/broadcast-receivers/do-something-when-the-phone-rings/\"相对=nofollow>链接2
I got reference from this websites. Link 1 Link 2
推荐答案
继续我的谈话提问者作为answer.I刚开始一个新的Android应用程序,并在评论中摸索出像我说..清单如下
Continuing my conversation with asker as an answer.I Just started a new Android application and worked out like i have said in the comments.. The manifest looks like this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dj.randomtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".CallDetector">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
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>
该CallDetector类看起来像这样(UR code):
The CallDetector class looks like this(ur code):
public class CallDetector extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
Toast.makeText(context, "Phone Is Ringing", Toast.LENGTH_LONG).show();
}
if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))
{
Toast.makeText(context, "Call Recieved", Toast.LENGTH_LONG).show();
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE))
{
Toast.makeText(context, "Phone Is Idle", Toast.LENGTH_LONG).show();
}
}
}
这篇关于广播接收器不扣除电话 - Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!