gpt4 book ai didi

android - 无法解决方法 setLatestEventInfo

转载 作者:IT老高 更新时间:2023-10-28 13:24:07 25 4
gpt4 key购买 nike

我正在处理通知,我必须使用 setLatestEventInfo。但是,Android Studio 显示以下错误消息:

cannot resolve method setLatestEventinfo

这是我的代码 fragment :

private void createNotification(Context context, String registrationID) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,"Registration Successfull",System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent intent = new Intent(context,RegistrationResultActivity.class);
intent.putExtra("registration_ID",registrationID);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent,0);
notification.setLatestEventInfo(context,"Registration","Successfully Registered",pendingIntent);
}

或者如果他们是另一种方式,请建议我这样做。

最佳答案

下面是一个使用通知的简单示例,请仔细阅读,希望对您有所帮助!

MainActivity.java

public class MainActivity extends ActionBarActivity {

Button btnShow, btnClear;
NotificationManager manager;
Notification myNotication;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initialise();

manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

btnShow.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
//API level 11
Intent intent = new Intent("com.rj.notitfications.SECACTIVITY");

PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 1, intent, 0);

Notification.Builder builder = new Notification.Builder(MainActivity.this);

builder.setAutoCancel(false);
builder.setTicker("this is ticker text");
builder.setContentTitle("WhatsApp Notification");
builder.setContentText("You have a new message");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentIntent(pendingIntent);
builder.setOngoing(true);
builder.setSubText("This is subtext..."); //API level 16
builder.setNumber(100);
builder.build();

myNotication = builder.getNotification();
manager.notify(11, myNotication);

/*
//API level 8
Notification myNotification8 = new Notification(R.drawable.ic_launcher, "this is ticker text 8", System.currentTimeMillis());

Intent intent2 = new Intent(MainActivity.this, SecActivity.class);
PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 2, intent2, 0);
myNotification8.setLatestEventInfo(getApplicationContext(), "API level 8", "this is api 8 msg", pendingIntent2);
manager.notify(11, myNotification8);
*/

}
});

btnClear.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
manager.cancel(11);
}
});
}

private void initialise() {
btnShow = (Button) findViewById(R.id.btnShowNotification);
btnClear = (Button) findViewById(R.id.btnClearNotification);
}
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
android:id="@+id/btnShowNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Notification" />

<Button
android:id="@+id/btnClearNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear Notification" />

</LinearLayout>

以及点击通知会打开的 Activity ,

public class SecActivity extends Activity {

}

关于android - 无法解决方法 setLatestEventInfo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32345768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com