簡體   English   中英

如何在API 23+中解決setLatestEventInfo

[英]How can setLatestEventInfo be resolved in API 23+

我正在創建一個模擬推送通知應用程序。 從用戶那里獲取輸入,並在設備上顯示本地推送通知。 據我所知, 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);
        }
    });
}

謝謝 :)

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

例如, 本書 示例項目中的此方法使用以上所有方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM