Question

I'am new to android apps...

I wish to create as easiest as possible android background (no-gui) app based on ContentObserver that will monitor any sms in/out activities and notify me about this fact.

I've got this:

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

I know that for ContentObserver I need to create a service in AndroidManifest.xml:

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

and grant rights to app boot at start:

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

But still sth is wrong...

I'am getting: Unable to instantiate activity ComponentInfo{com.example.smsnotify/com.example.smsnotify.SMSNotifyActivity}: java.lang.InstantiationException: com.example.smsnotify.SMSNotifyActivity

My whole 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>
Was it helpful?

Solution

You have a bit of confusion with Service, BroadcastReceiver, ContentObserver and Activity.

Create a Service (extend from Service or IntentService) and put your ContentObserver in it as an inner class.

Remove the <activity> tag from your manifest, you don't need that.

The <receiver> element in your manifest should point to a BroadcastReceiver extending class. Try extending a BroadcastReceiver, call it SMSNotifyStarter, put this in your manifest:

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

And when you get to broadcast of a boot, start your Service.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top