【问题标题】:How can setLatestEventInfo be resolved in API 23+如何在 API 23+ 中解析 setLatestEventInfo
【发布时间】:2015-10-24 21:16:48
【问题描述】:

我正在创建一个模拟推送通知应用。 它接受用户的输入并在设备上显示本地推送通知。 据我所知setLatestEventInfo 方法在新的API (23+) 级别中已停用。 我想知道代码的可能补丁是什么。 下面是代码:

public class MainActivity extends ActionBarActivity {
EditText ed1,ed2,ed3;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ed1=(EditText)findViewById(R.id.editText);
    ed2=(EditText)findViewById(R.id.editText2);
    ed3=(EditText)findViewById(R.id.editText3);
    Button b1=(Button)findViewById(R.id.button);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String tittle=ed1.getText().toString().trim();
            String subject=ed2.getText().toString().trim();
            String body=ed3.getText().toString().trim();

            NotificationManager notif=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notify=new Notification(R.drawable.icon,tittle,System.currentTimeMillis());
            PendingIntent pending= PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);

            notify.setLatestEventInfo(getApplicationContext(),subject,body,pending); //cannot resolve 'setLatestEventInfo' method 
            notif.notify(0, notify);
        }
    });
}

谢谢你:)

【问题讨论】:

  • 为什么需要它? AFAIK,setLatestEventInfo() 能做的一切都由NotificationCompat.Builder 处理。
  • 是的,正是...谢谢...但是我如何在我的代码中实现它?
  • 先生理解起来越来越复杂了:(

标签: java android notifications push-notification


【解决方案1】:

使用NotificationCompat.Builder,以及setSmallIcon()setTicker()setContentTitle()setContentText()setContentIntent()

例如,来自this sample project 的这个方法来自this book 使用上述所有方法:

  private void raiseNotification(String mimeType, File output,
                                 Exception e) {
    NotificationCompat.Builder b=new NotificationCompat.Builder(this);

    b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL);

    if (e == null) {
      b.setContentTitle(getString(R.string.download_complete))
       .setContentText(getString(R.string.fun))
       .setSmallIcon(android.R.drawable.stat_sys_download_done)
       .setTicker(getString(R.string.download_complete));

      Intent outbound=new Intent(Intent.ACTION_VIEW);

      outbound.setDataAndType(Uri.fromFile(output), mimeType);

      b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0));
    }
    else {
      b.setContentTitle(getString(R.string.exception))
       .setContentText(e.getMessage())
       .setSmallIcon(android.R.drawable.stat_notify_error)
       .setTicker(getString(R.string.exception));
    }

    NotificationManager mgr=
        (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    mgr.notify(NOTIFY_ID, b.build());
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    相关资源
    最近更新 更多