0

我正在android中开发一个应用程序并使用BroadcastReceiver类来覆盖onReceive方法。但是当短信来临时,我的应用程序意外关闭。代码如下所示。

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();

}
}

我的清单文件是

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

但是在运行应用程序时,它意外关闭,我从 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

有什么问题吗?

提前致谢。

4

2 回答 2

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

那是你的错误onReceive。您不能创建这样的实例,Activity因为它不是Class.

于 2012-10-10T18:23:50.787 回答
0

而不是:BlinkMe 闪烁 = new BlinkMe(); 闪烁.RedFlashLight();

做:BlinkMe.RedFlashLight()

并确保将该方法设为静态(尝试将变量设为静态)

于 2013-08-20T22:22:10.227 回答