Search code examples
androidbroadcastreceiver

Application closing unexpectedly


I am developing an application in android and using BroadcastReceiver class to override the method onReceive. But my application is closing unexpectedly when an SMS comes. The code is shown below.

package com.jb.blinkme;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsMessage;
import android.view.Menu;
import android.widget.Toast;

public class BlinkMe extends Activity {

private static final int LED_NOTIFICATION_ID = 33;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_blink_me);
    RedFlashLight();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_blink_me, menu);
    return true;
}
public void RedFlashLight() {
    NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
    Notification notif = new Notification();
    notif.ledARGB = 0xF7FE2E;
    notif.flags = Notification.FLAG_SHOW_LIGHTS;
    notif.ledOnMS = 1000; 
    notif.ledOffMS = 1000; 
    nm.notify(LED_NOTIFICATION_ID, notif);
    Handler myHandler = new Handler();
    myHandler.postDelayed(mClearLED_Task, 10000);
}
public void ClearLED() {
    NotificationManager nm = ( NotificationManager ) getSystemService( NOTIFICATION_SERVICE );
    nm.cancel( LED_NOTIFICATION_ID );
}
public Runnable mClearLED_Task = new Runnable() {
    public void run() {
        ClearLED();
    }
};

}

package com.jb.blinkme;
import android.content.BroadcastReceiver;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver {

/**
 * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
 */
@Override
public void onReceive(Context context, Intent intent) {
    BlinkMe blink = new BlinkMe();
    blink.RedFlashLight();

}
}

and my manifest file is

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jb.blinkme"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:debuggable="true" >
    <activity
        android:name=".BlinkMe"
        android:label="@string/title_activity_blink_me" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:name=".SMSReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

But while running the application, It is closing unexpectedly and I am getting the following error from LogCat.

10-10 23:38:53.410: E/AndroidRuntime(6248): FATAL EXCEPTION: main
10-10 23:38:53.410: E/AndroidRuntime(6248): java.lang.RuntimeException: Unable to start receiver com.jb.blinkme.SMSReceiver: java.lang.IllegalStateException: System services not available to Activities before onCreate()
10-10 23:38:53.410: E/AndroidRuntime(6248):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2146)
10-10 23:38:53.410: E/AndroidRuntime(6248):     at android.app.ActivityThread.access$1500(ActivityThread.java:127)
10-10 23:38:53.410: E/AndroidRuntime(6248):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208)
10-10 23:38:53.410: E/AndroidRuntime(6248):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 23:38:53.410: E/AndroidRuntime(6248):     at android.os.Looper.loop(Looper.java:137)
10-10 23:38:53.410: E/AndroidRuntime(6248):     at android.app.ActivityThread.main(ActivityThread.java:4441)
10-10 23:38:53.410: E/AndroidRuntime(6248):     at java.lang.reflect.Method.invokeNative(Native Method)
10-10 23:38:53.410: E/AndroidRuntime(6248):     at java.lang.reflect.Method.invoke(Method.java:511)
10-10 23:38:53.410: E/AndroidRuntime(6248):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-10 23:38:53.410: E/AndroidRuntime(6248):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-10 23:38:53.410: E/AndroidRuntime(6248):     at dalvik.system.NativeStart.main(Native Method)
10-10 23:38:53.410: E/AndroidRuntime(6248): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
10-10 23:38:53.410: E/AndroidRuntime(6248):     at android.app.Activity.getSystemService(Activity.java:3989)
10-10 23:38:53.410: E/AndroidRuntime(6248):     at com.jb.blinkme.BlinkMe.RedFlashLight(BlinkMe.java:32)
10-10 23:38:53.410: E/AndroidRuntime(6248):     at com.jb.blinkme.SMSReceiver.onReceive(SMSReceiver.java:18)
10-10 23:38:53.410: E/AndroidRuntime(6248):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2139)
10-10 23:38:53.410: E/AndroidRuntime(6248):     ... 10 more

Is there anything wrong?

Thanks in advance.


Solution

  • BlinkMe blink = new BlinkMe();
    blink.RedFlashLight(); 
    

    That is wrong in your onReceive. You can't create an instance like this for Activity since it is not a Class.