【问题标题】:How to detect Incoming call in background如何在后台检测来电
【发布时间】:2013-02-18 08:11:52
【问题描述】:

我正在尝试在应用程序上创建。我需要检测我是否接到任何电话然后获取号码,但这所有过程都应该在后台发生。
后台工作是后半部分,但现在我正在尝试启动我的应用程序,但它崩溃了。
你能告诉我如何在后台调用broadcastreciver吗? 这是我的 MainActivity.java

public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstancestate)
{
    super.onCreate(savedInstancestate);
    setContentView(R.layout.activity_main);

}
public void broadcastIntent(View view)
{
      Intent intent = new Intent();
    intent.setAction("com.app.callrecord.MyBroadcastReceiver");
    sendBroadcast(intent);
} 
}

Broadcastreceiver.java

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
   Bundle bundle = intent.getExtras();
   if (bundle == null)
      return;
   String phoneNumber = null;

   // Incoming call
   String state = bundle.getString(TelephonyManager.EXTRA_STATE);
   if ((state != null) &&  (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))) {
       phoneNumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);  
      // callToast(phoneNumber);

   }
   // Outgoing call
   else if (state == null) {        
       Intent i = new Intent(context,RecordHistory.class);  
       intent.putExtra("phonenumber", phoneNumber);
       i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
       context.startActivity(i);  
      // Here: do something with the number
   }  
 }
}

这是我的清单文件

 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name="com.app.callrecord.MainActivity">
<intent-filter>
    <action android:name="android.intent.action.PHONE_STATE"></action>
    <action android:name="android.intent.action.NEW_OUTGOING_CALL"></action>
</intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".RecordHistory"></activity>
</application>

这是我的异常日志

 02-18 13:36:17.240: E/AndroidRuntime(9397): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app.callrecord/com.app.callrecord.MyBroadCastReciever}: java.lang.ClassNotFoundException: com.app.callrecord.MyBroadCastReciever
 02-18 13:36:17.240: E/AndroidRuntime(9397):    at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
 02-18 13:36:17.240: E/AndroidRuntime(9397):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2278)
 02-18 13:36:17.240: E/AndroidRuntime(9397):    at android.app.ActivityThread.access$600(ActivityThread.java:142)
02-18 13:36:17.240: E/AndroidRuntime(9397):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)

给我任何参考。

【问题讨论】:

    标签: android background broadcastreceiver phone-call


    【解决方案1】:

    您的清单中有错误。以下行错误地引用了活动:

    <receiver android:name="com.app.callrecord.MainActivity">
    

    它应该是指接收者:

    <receiver android:name="SOME_PACKAGE_HERE.MyBroadcastReceiver">
    

    编辑:

    如果广播接收器没有被呼叫,您可能没有权限检测来电和去电。您的清单中需要 android.permission.READ_PHONE_STATEandroid.permission.PROCESS_OUTGOING_CALLS 权限。

    【讨论】:

    • 感谢您的回答,但是当我启动应用程序时可以在我的手机上拨打电话时,我没有收到任何我用来显示号码的 toast ...
    • 这可能是由于缺少权限。我编辑了答案。
    【解决方案2】:

    我认为问题在于您在清单中声明了错误的名称。您已将其声明为

    <receiver android:name="com.app.callrecord.MainActivity">
    

    应该是这样的

    <receiver android:name="com.app.callrecord.MyBroadCastReciever">
    

    【讨论】:

    • 感谢您的回答,但是当我启动应用程序时可以在我的手机上拨打电话时,我没有收到任何我用来显示号码的 toast ...
    • callToast(phoneNumber) 部分似乎已被注释掉。这可能是原因。
    猜你喜欢
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    相关资源
    最近更新 更多