1

バックグラウンド サービスを (独自のプロセスで) 作成していますが、それを機能させるのに苦労しています。アプリケーションの起動時に起動しようとしていますが、意図的にサービスを開始できないというエラーがログに記録されています。私はフォーラム、例、(およびグーグル)を調べてきましたが、私が間違っていることを見つけることができませんでした。

これが私が得ているエラーです:
E/AndroidRuntime(1398): java.lang.RuntimeException: Unable to start service com.test.alarms.AlarmService@41550cb0 with Intent { cmp=xxxx }: java.lang.NullPointerException

私が持っている活動では:

startService(new Intent(AlarmService.class.getName()));

サービス クラスは次のとおりです。

package com.test.alarms;


public class AlarmService extends Service{

Context context; 

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
//code to execute when the service is first created
}

@Override
public void onDestroy() {
//code to execute when the service is shutting down
}

@Override
public void onStart(Intent intent, int startid) {
//code to execute when the service is starting up
    Intent i = new Intent(context, StartActivity.class);
    PendingIntent detailsIntent = PendingIntent.getActivity(this, 0, i, 0);

    NotificationManager notificationSingleLine = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notificationDropText = new Notification(R.drawable.ic_launcher, "Alarm for...", System.currentTimeMillis());

    CharSequence from = "Time for...";
    CharSequence message = "Alarm Text";        
    notificationDropText.setLatestEventInfo(this, from, message, detailsIntent);

    notificationDropText.vibrate = new long[] { 100, 250, 100, 500};        
    notificationSingleLine.notify(0, notificationDropText);
}

}

マニフェスト ファイルには次のものがあります。

<service
        android:name=".AlarmService"
        android:process=":remote">
        <intent-filter>
            <action android:name="com.test.alarms.AlarmService"/>
        </intent-filter>
    </service>

ありがとう、

4

2 に答える 2

5

おそらく問題は、OnCreate と OnDestroy をオーバーライドしたが、super.Oncreate() と super.onDestroy() を呼び出していないことです。

これがうまくいかない場合は試してください

startService(new Intent(context , AlarmService.class));

編集:onStartでもこれを使用してください

Intent i = new Intent(this, StartActivity.class);

それ以外の

Intent i = new Intent(context, StartActivity.class);
于 2012-08-25T18:21:12.223 に答える
4

ドキュメントによると、渡すインテントが null (プロセスが強制終了された後に再起動するため) であるか、そうでないか (通常の操作) を確認する必要があります。

より良いデザイン、

public int onStartCommand(Intent intent, int flags, int startId) {
    if(intent != null){
        handleCommand(intent);
    }
    // We want this service to continue running until it is explicitly
    // stopped, so return sticky.
    return START_STICKY;
}

ここでもっと読む

于 2013-11-12T15:56:15.363 に答える