我对Android开发非常陌生。我在运行我的应用程序时遇到问题。我正在尝试通过使用BroadcastReceiver为来电通知举杯。但是我从清单com.sortinousn.Salesforce.calltracker中得到了这个错误,无法分配给android.app.Activity。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sortinousn.salesforcecalltracker"
android:versionCode="1"
android:versionName="1.0" >

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.sortinousn.salesforcecalltracker.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>

    <receiver android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>


</application>
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-    permission>

</manifest>


主要活动

public class MainActivity extends BroadcastReceiver {

Context pcontext;

public void onReceive(Context context, Intent intent) {

    try {
        // TELEPHONY MANAGER class object to register one listner
        TelephonyManager tmgr = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);

        //Create Listner
        MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

        // Register listener for LISTEN_CALL_STATE
        tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    } catch (Exception e) {
        Log.e("Phone Receive Error", " " + e);
    }

}

private class MyPhoneStateListener extends PhoneStateListener {

    public void onCallStateChanged(int state, String incomingNumber) {

        Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);

        if (state == 1) {

            String msg = "New Phone Call Event. Incomming Number :     "+incomingNumber;
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(pcontext, msg, duration);
            toast.show();

        }
    }
}
}

最佳答案

我不太明白为什么您将MainActivity类作为activityreceiver ...这些是不一样的。

正如我在您的代码中看到的那样,您没有任何活动,因为您在MainActivity类中扩展了BroadcastReceiver ...所以...只需通过扩展另一个具有Activity的类(或其他类)来进行真正的活动),并在清单中将此类声明为活动,就像这样:Activity

package fr.zwedge.kingwarrior;

import android.app.*;
import android.os.Bundle;

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        // Tell me if I'm wrong for what's above
        BroadcastReceiver receiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                try {
                    // TELEPHONY MANAGER class object to register one listner
                    TelephonyManager tmgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

                    //Create Listner
                    MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

                   // Register listener for LISTEN_CALL_STATE
                   tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

               } catch (Exception e) {
                   Log.e("Phone Receive Error", " " + e);
               }
           }
       }
   }
   public /* can't be private here */ class MyPhoneStateListener extends PhoneStateListener {
      public void onCallStateChanged(int state, String incomingNumber) {
         Log.d("MyPhoneListener",state+"   incoming no:"+incomingNumber);

         if (state == 1) {
            String msg = "New Phone Call Event. Incomming Number :     "+incomingNumber;
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(pcontext, msg, duration);
            toast.show();
         }
      }
  }


Manifest

<application android:allowBackup="true"
    android:label="King Warrior"
    android:icon="@drawable/ic_launch">
    <activity android:name=".Main"
        android:label="What you want"
        android:icon="@drawable/ic_launch">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


希望能帮助到你,
黑暗之球60。

=================================

或者,如果您不需要应用程序的任何界面,则可以只删除清单中的<activity />标记。

10-04 18:02