【问题标题】:Issue with Notification Manager on androidAndroid上的通知管理器问题
【发布时间】:2013-03-19 07:21:07
【问题描述】:

我正在尝试使用按钮进行通知,但不推荐使用 Notification 和 setLatestEventInfo。

两个错误:

1.构造函数 Notification(int, CharSequence, long) 已弃用Notification notify = new Notification(android.R.drawable.stat_notify_more, "Hello all", System.currentTimeMillis());

2.Notification类型中的方法setLatestEventInfo(Context, CharSequence, CharSequence, PendingIntent)不适用于参数(Context, CharSequence, CharSequence, Intent) notify.setLatestEventInfo(context, title, details, intent);

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new View.OnClickListener() {   
        @Override
        public void onClick(View v) {
            NotificationManager ns = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notify = new Notification(android.R.drawable.stat_notify_more, "Hello all", System.currentTimeMillis());
            Context context = MainActivity.this;
            CharSequence title ="you have be notified";
            CharSequence details = "Continue your work";
            Intent intent = new Intent(context,MainActivity.class);
            PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
            notify.setLatestEventInfo(context, title, details, intent);
            ns.notify(0,notify);


        }
    });
}

API 级别:

       android:minSdkVersion="11"
       android:targetSdkVersion="17"

有什么选择?

【问题讨论】:

    标签: android notifications notify notificationmanager


    【解决方案1】:

    1.构造函数在 api 级别 11 中已弃用。因此您应该使用Notification.Builder

    例如

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

    2。在您的代码中,您正在传递意图而不是在 setLatestEventInfo 中挂起

    ....
    Intent intent = new Intent(context,MainActivity.class);
            PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);
            notify.setLatestEventInfo(context, title, details, pending);
            ns.notify(0,notify);
    ....
    

    【讨论】:

    • 例子中必须是Bitmap类的对象。
    • 但由于向后兼容,我们不能这样做。我们的应用程序应该可以在 4.0、4.1 和 4.2.2 上运行。所以我用 4.2.2 编译了它,并且应用程序可以在所有设备上运行。并在 manifest.xml 添加 android:minSdkVersion="11" android:targetSdkVersion="17"
    【解决方案2】:
    notificationManager =
        (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
       myNotification = new Notification(R.drawable.icon,
         "Notification!",
         System.currentTimeMillis());
       Context context = getApplicationContext();
       String notificationTitle = "Exercise of Notification!";
       String notificationText = "http://niravranpara.blogspot.com/";
       Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
       PendingIntent pendingIntent
         = PendingIntent.getActivity(AndroidNotification.this,
           0, myIntent,
           Intent.FLAG_ACTIVITY_NEW_TASK);
       myNotification.defaults |= Notification.DEFAULT_SOUND;
       myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
       myNotification.setLatestEventInfo(context,
          notificationTitle,
          notificationText,
          pendingIntent);
       notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
    
      }
    

    【讨论】:

      【解决方案3】:

      那些构造函数和方法已被弃用。所以你应该改用通知生成器。

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

      【讨论】:

        【解决方案4】:

        代替System.currentTimeMillis() 试试java.lang.System.currentTimeMillis()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-25
          相关资源
          最近更新 更多