0

簡単なサービスを作成し、これを通知しています。通知用のクラスを書いています。すべてのコードを記述した後、3行に赤い色で下線が引かれます。1つはgetSystemService(ns);14行目のこの関数で、2つ目はgetApplicationContext();20行目のこの関数で、3つ目は最初の行と同じ機能ですが31行目でcancelNotification()機能しています。これが私の完全なコードです

package com.zafar.batterynotify;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class Notify {
    private static final int NOTIFICATION_ID = 1;

    public void initNotification() {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "Service Started";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);
        notification.flags = Notification.FLAG_ONGOING_EVENT;
        Context context = getApplicationContext();
        CharSequence contentTitle = "Ongoing service";
        CharSequence contentText = "This is service is ongoing";
        Intent notificationIntent = new Intent(context, BatteryNotify.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }

    public void cancelNotification() {
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
        mNotificationManager.cancel(NOTIFICATION_ID);
    }
}

更新されたコードを編集する

私のサービスクラス

package com.zafar.batterynotify;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class BatteryService extends Service {
Notify notification = new Notify();
String ns = Context.NOTIFICATION_SERVICE;
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    notification.initNotification(Context.NOTIFICATION_SERVICE);
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    return START_STICKY;
}

public void onDestroy() {
    super.onDestroy();
    notification.cancelNotification(Context.NOTIFICATION_SERVICE);
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}

}

クラスに通知

package com.zafar.batterynotify;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class Notify {
private static final int NOTIFICATION_ID = 1;

public void initNotification(Context actContext) {
    //String ns = Context.NOTIFICATION_SERVICE;
    //Context context = actContext.getApplicationContext();
    NotificationManager mNotificationManager = actContext.getSystemService(ns);
    int icon = R.drawable.ic_launcher;
    CharSequence tickerText = "Service Started";
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, when);
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    Context context = actContext.getApplicationContext();
    CharSequence contentTitle = "Ongoing service";
    CharSequence contentText = "This is service is ongoing";
    Intent notificationIntent = new Intent(context, BatteryNotify.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, notification);
}

public void cancelNotification(Context actContext) {
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = actContext.getSystemService(ns);
    mNotificationManager.cancel(NOTIFICATION_ID);
}
}
4

2 に答える 2

0

アクティビティまたはサービスの呼び出しからクラスにコンテキストを渡し、それを使用します。

    public void initNotification(Context actContext) {
    //...
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = actContext.getSystemService(ns);
    //...
    }

    public void cancelNotification(Context actContext) {
        //...
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = actContext.getSystemService(ns);
        //...
    }
于 2012-05-12T08:38:46.600 に答える
0

を使用しようとしないでくださいgetApplicationContext()。代わりにMyApplication、から継承されたクラスを作成Applicationし、そのクラス内で次のようにします。

public class MyApplication extends Application {
    private static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        .........
    }

    public static Context getContext() {
        return instance;
    }

その後、コンテキストが必要で、うそをついてMyApplication.getContext()いることがなければ、どこでも使用できます。Activity

于 2012-05-12T08:45:38.107 に答える