我是初学者android开发人员。
我正在尝试从Activity调用BroadcastReceiver,因为如果有人给我打电话,我需要更新条目。

这是我打电话给BroadcastReceiver的活动。

源代码:

public class CalendarCall extends Activity {

    private static final String TAG = "CalendarCall";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Intent i = new Intent(this, MyBroadcastReceiver.class);
        startActivity(i);
    }
    // ......
}


MyBroadcastReceiver.class:

package org.example.calendarcall;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
         Bundle extras = intent.getExtras();
         if (extras != null) {
                String state = extras.getString(TelephonyManager.EXTRA_STATE);
                Log.w("DEBUG", state);
                if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                    String phoneNumber = extras
                            .getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
                    Log.w("DEBUG", phoneNumber);
                }
         }
     }
}


表现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="org.example.calendarcall"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".CalendarCall"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="MyBroadcastReceiver">
        </receiver>
    </application>
    <uses-sdk android:minSdkVersion="8" />

<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
</manifest>


错误:

04-21 12:37:40.821: ERROR/AndroidRuntime(793): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.calendarcall/org.example.calendarcall.CalendarCall}: android.content.ActivityNotFoundException: Unable to find explicit activity class {org.example.calendarcall/org.example.calendarcall.MyBroadcastReceiver};


我认为问题是清单中的声明,我也不知道我是否可以使用以下方法从Activity调用BroadcastReceiver:

Intent i = new Intent(this, MyBroadcastReceiver.class);
startActivity(i);

最佳答案

BroadCastReceiver从调用sendBroadcast()方法的发送者那里接收意图。如果您希望在有人呼叫您时调用MyBroadcastReceiver,则可以添加过滤器android.intent.action.PHONE_STATE,例如:

  <receiver android:name=".MyBroadcastReceiver">

    <intent-filter>

      <action android:name="android.intent.action.PHONE_STATE" />

    </intent-filter>

</receiver>


您实际上并不需要CalendarCall活动。

如果要将信息从广播接收器传输到活动中,请参见this stackoverflow question

关于android - 从Activity调用BroadcastReceiver,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5743024/

10-13 04:41