开发者

Call BroadcastReceiver from Activity

I am beginner android developer.

I am trying to call BroadcastReceiver from Activity because I need to update entry if somebody call me.

This is the activity where I am calling BroadcastReceiver.

Source code:

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

Manifest:

<?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>
                <act开发者_如何学编程ion 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> 

Error:

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

I think the problem is declaraction in Manifest and i also dont know if i can call BroadcastReceiver from Activity using:

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


A BroadCastReceiver receives intents from sender's who's calling the sendBroadcast() method. If you would like your MyBroadcastReceiver to be invoked when someone calls you, you could add the filter android.intent.action.PHONE_STATE like:

  <receiver android:name=".MyBroadcastReceiver">

    <intent-filter>

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

    </intent-filter>

</receiver>

You don't really need your CalendarCall activity.

If you want to transfer information from your broadcast receiver to your activity, see this stackoverflow question.


you can call a broadcaste reciever from activity. you need to register and unregister the reciever and you need to call sendbroadcastereciver() also in your code

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜