sách gpt4 ăn đã đi

android - 如何在android中创建通知?

In lại Tác giả: Walker 123 更新时间:2023-11-29 15:08:30 29 4
mua khóa gpt4 giày nike

我正在尝试在 android 中创建一个简单 通知。目的是在发生某些事情时在手机屏幕顶部显示一个小图标。然后我可以点击将启动应用程序或将其置于前台的通知。

出于测试目的,我正在关注 cái này难以理解且不简单的示例,还发现了 cái này我得到的第一个错误的条目。

这里是代码:

    Notification mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon1)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainActivity.class);

// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
int mId = 1001;
mNotificationManager.notify(mId, mBuilder.build());

这是我要导入的内容:

...
import android.app.Notification;
import android.app.NotificationManager;
....
import android.support.v7.app.ActionBar;
//import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.support.v7.widget.Toolbar;

当前错误是

Error:(117, 32) error: incompatible types
required: Notification
found: Builder

Làm thế nào để giải quyết vấn đề này?

câu trả lời hay nhất

改变

Notification mBuilder 

NotificationCompat.Builder mBuilder 

因为你正在实例化 new NotificationCompat.Builder(this) 你不能将它分配给 Notification sự vật

关于android - 如何在android中创建通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34381643/

29 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress