2012-05-12 45 views
0

我创建了一个简单的服务,现在我正在为此发出通知。我正在写一个通知类。在写完所有代码后,三条线用红色标出,一个是这个功能getSystemService(ns);在第14行,第二个是这个第getApplicationContext();在第20行,第三个功能与第一个功能相同,但在第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); 
} 
} 

回答

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); 
     //... 
    } 
+0

请参阅我的更新。我改变了它,但仍然不起作用。 Noe红色下划线分为两行。在第15行getSystemService(ns);仅在括号内的ns下,第二个是在cancelNotification函数行号32中actContext.getSystemService(ns); – 2619

+0

不要评论'String ns = Context.NOTIFICATION_SERVICE;' – breceivemail

+0

是的,现在已经修复了,但现在问题在服务类中。现在,我所调用的函数,即initNotification和cancelNotification都是用红线单引号。当我将鼠标悬停在此上时,它将initNotification从initNotification(Context)更改为initNotification(String)。 – 2619

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