我想在我的android應用程序中設置一個通知,它只會說「它工作」,但我需要我的應用程序具有兼容性一直到API 1。米真的很困惑如何做到這一點。有舊的教程已過時,並且有新的教程不支持較舊的API級別。根據這個SO的問題,我應該使用NotificationCompat.Builder。有一個我正在使用的例子,但我沒有完全理解代碼。試圖在Android中構建一個簡單的通知
出於這種代碼:
Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
YOUR_PI_REQ_CODE, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.some_img)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
.setTicker(res.getString(R.string.your_ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.your_notif_title))
.setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();
nm.notify(YOUR_NOTIF_ID, n);
我得到下紅線:ctx
,YOUR_PI_REQ_CODE
和YOUR_NOTIF_ID
嗯....任何想法,爲什麼它不適用於API級別3? – EGHDK
根據您所鏈接的SO答案,您使用的代碼僅在API v4及更新版本中受支持。這就是說......爲什麼你覺得你需要支持比這更老的?任何使用舊手機的人都不太可能安裝新的應用程序。 – mah
Bah。我想你是對的哈哈哈。謝謝。我很欣賞這種努力。快速提問。你知道什麼方法使通知「不可取消」嗎?語音郵件通知如何工作。在您檢查所有語音郵件之前,它不會消失。謝謝 – EGHDK