2013-09-26 34 views
1

如何解决此问题?如果我用this替换它。它在建造者上创造了更多的错误。我如何解决它?Notification Builder无法接受“this”作为“context”的替代,为什么?

java类中的所有代码。在这种情况下,功能围绕onStartCommand旋转。

public class MyNotificationService extends Service { 

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

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
     Toast.makeText(this, "OnCreate()", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     Toast.makeText(this, "OnDestroy()", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "OnStartCommand()", Toast.LENGTH_SHORT).show(); 
     NotificationManager notificationmanager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     Intent notificationintent = new Intent(this, Reminder_2.class); 
     PendingIntent pendingintent = PendingIntent.getActivity(this, 0, notificationintent, 0); 
     int icon=R.drawable.ic_launcher; 
     long when=System.currentTimeMillis(); 
     Notification.Builder builder = new Builder(this); 
     builder.setContentIntent(pendingintent); 
     builder.setAutoCancel(true); 
     builder.setSmallIcon(icon); 
     builder.setWhen(when); 
     builder.setTicker("Notification"); 
     builder.setContentTitle("Title"); 
     builder.setContentText("Content"); 
     Notification notification = builder.build(); 
     notificationmanager.notify(033, notification); 
     return super.onStartCommand(intent, flags, startId); 
    } 


} 

回答

2
// try this 
Notification.Builder builder = new Notification.Builder(this); 
+0

这是最接近的答案。谢谢。但我能弄明白。在这种情况下,我需要使用NotificationCompat.Builder。 NotificationCompat.Builder建设者=新NotificationCompat.Builder(本) \t \t .setContentIntent(的PendingIntent) \t \t .setAutoCancel(真) \t \t .setSmallIcon(图标) \t \t .setWhen(时)...此代码工作,但是再次,谢谢你的答案! –

1

使用getApplicationContext()而不是this

+0

“getApplicationContext()”不起作用。就像“这个”一样,它也造成了同样的问题。 –

+0

“this”的哪个实例正是导致此问题的原因?我刚刚意识到你的代码中有几个'this's。 – nhgrif

+0

与构建器相关的任何内容都包含错误。该行在使用getApplicationContext()后包含错误; Notification.Builder builder = new Builder(getApplicationContext()); \t \t builder.setContentIntent(pendingintent); \t \t builder.setAutoCancel(true); \t \t builder.setSmallIcon(icon); \t \t builder.setWhen(when); \t \t builder.setTicker(“Notification”); \t \t builder.setContentTitle(“Title”); \t \t builder.setContentText(“Content”); \t \t通知通知= builder.build(); –

0

我想你已经使用了错误的生成器类,请尝试以下,

Notification noti = new Notification.Builder(this) 
    .setContentTitle("New mail from " + sender.toString()) 
    .setContentText(subject) 
    .setSmallIcon(R.drawable.new_mail) 
    .setLargeIcon(aBitmap) 
    .build(); 

参考: android developer site, Notification.Builder

希望可以帮到你。

相关问题