ブロードキャスト受信機を実装して、アンインストール、インストール、電話の着信時にイベントを取得しようとしています。私はAndroidManifest.xmlにインテントフィルタを登録しましたが、私の携帯電話でアプリケーションを実行すると、ランタイム例外がスローされます。Android:ブロードキャスト受信者がjava.lang.RuntimeExceptionをスローする
マイコード:
package com.local.broadcast_receiver;
import android.content.BroadcastReceiver;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
public class MainActivity extends android.content.BroadcastReceiver {
static boolean flag = false;
static long start_time_incoming = 0;
static long start_time_outgoing = 0;
static long end_time;
@Override
public void onReceive(Context ctx, Intent intent){
String action = intent.getAction();
if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE")) {
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_INCOMING_NUMBER)) {
start_time_incoming = System.currentTimeMillis();
}else if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)) {
start_time_outgoing = System.currentTimeMillis();
}
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)) {
end_time = System.currentTimeMillis();
}
if (start_time_incoming != 0) {
//Total time talked =
long total_time = end_time - start_time_incoming;
//Store total_time somewhere or pass it to an Activity using intent
Log.i("Broadcast", "Last incoming call duration:" + total_time);
}else if (start_time_outgoing != 0) {
//Total time talked =
long total_time = end_time - start_time_outgoing;
//Store total_time somewhere or pass it to an Activity using intent
Log.i("Broadcast", "Last outgoing call duration:" + total_time);
}
}else if(intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
Uri uri = intent.getData();
String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
Log.i("Broadcast", "name of package removed is:" + pkg);
}else if(intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
Uri uri = intent.getData();
String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
Log.i("Broadcast","name of package Added is:" + pkg);
}
}
}
と私のAndroidManifest.xmlは以下のようになります -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.local.broadcast_receiver">
<uses-permission
android:name="android.permission.READ_CALL_LOG"
android:maxSdkVersion="25" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="25" />
<receiver android:name="MainActivity">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
と私はRuntimeExceptionを得る: -
使用可能なさまざまな記事を検索しようとしました java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.local.broadcast_receiver/com.local.broadcast_receiver.MainActivity}: java.lang.ClassCastException: com.local.broadcast_receiver.MainActivity cannot be cast to android.app.Activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2819)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.ClassCastException: com.local.broadcast_receiver.MainActivity cannot be cast to android.app.Activity
at android.app.Instrumentation.newActivity(Instrumentation.java:1086)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2809)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
をここでトピックについては "android.app.Activityにキャストできません"が、ヒントは得られません。誰かが間違っていることを指摘できますか、私の電話でこのコードを実行するために何をすべきですか?
おかげ
を参照し、適切にあなたのマニフェストを更新 – Shark