1

我该如何解决这个问题?如果我将其替换为this. 它在构建器上产生了更多错误。我该如何解决?

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


}
4

3 回答 3

2
// try this
Notification.Builder builder = new Notification.Builder(this);
于 2013-09-26T03:53:26.760 回答
1

使用getApplicationContext()代替this

于 2013-09-26T03:10:37.277 回答
0

我认为您使用了错误的 Builder 类,请在下面尝试,

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

参考: android 开发者网站,Notification.Builder

希望能帮到你。

于 2013-09-26T03:24:53.210 回答