java - 基于 ContentObserver 的应用程序的短信记录器 : how to create AndroidManifest. xml

标签 java android sms android-manifest contentobserver

我是 Android 应用新手...

我希望基于 ContentObserver 创建尽可能简单的 Android 后台(无 GUI)应用程序,该应用程序将监视任何短信输入/输出 Activity 并通知我这一事实。

我有这个:

import android.content.Context;
import android.database.ContentObserver;
import android.os.Handler;
import android.util.Log;

public class SMSNotifyActivity extends ContentObserver {
    private static String TAG ="SMSContentObserver";

    private Context MContext ;
    private Handler MHandler;

    public SMSNotifyActivity(Context Context, Handler Handler) {
        super(Handler);
        MContext = Context;
        MHandler = Handler;
    }

    @Override
    public void onChange(boolean selfChange) {
        Log.i(TAG, "The Sms Table Has Changed ") ;
    }
}

我知道对于 ContentObserver,我需要在 AndroidManifest.xml 中创建一个服务:

<service android:name=".SMSNotifyActivity" />

并授予应用程序在启动时启动的权限:

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

...

<receiver android:name=".SMSNotifyActivity" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

但是还是有问题......

我收到:无法实例化 Activity ComponentInfo{com.example.smsnotify/com.example.smsnotify.SMSNotifyActivity}:java.lang.InstantiationException:com.example.smsnotify.SMSNotifyActivity

我的整个AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.smsnotify"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SMSNotifyActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".SMSNotifyActivity" />
        <receiver android:name=".SMSNotifyActivity" >
           <intent-filter>
               <action android:name="android.intent.action.BOOT_COMPLETED" />
           </intent-filter>
        </receiver>
    </application>
</manifest>

最佳答案

您对 Service 有点困惑, BroadcastReceiver , ContentObserverActivity .

创建 Service (从 ServiceIntentService 扩展)并输入 ContentObserver作为内部类。

删除<activity> list 中的标记,您不需要它。

<receiver> list 中的元素应指向 BroadcastReceiver延伸类。 尝试延长 BroadcastReceiver ,将其命名为 SMSNotifyStarter,将其放入您的 list 中:

<receiver android:name=".SMSNotifyStarter" >
   <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED" />
   </intent-filter>
</receiver>

当您开始广播启动时,启动您的 Service .

关于java - 基于 ContentObserver 的应用程序的短信记录器 : how to create AndroidManifest. xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10192014/

相关文章:

android - Kivy:从主应用程序停止 android 服务

iphone - 在 iOS 7 中发送短信 - 从 iOS 6 升级后出现问题

android - 麻省理工学院 AppInventor : How to handle a large list?

java - 使用 Java 通过 USB gsm 调制解调器发送短信

android - React native 应用程序仅在发布版本时崩溃

java - Android Stduio 中无法出现 "Build APK"[libGDX]

java - 如何从 AES-256 切换到 AES-128?

java - BufferedWriter 用 MSB 1 写入字符

java - Findbugs:自定义检测器

android - ListView 显示多个选定项目