gpt4 book ai didi

java - 在 android 模拟器上按下按钮时发出通知

转载 作者:行者123 更新时间:2023-12-02 08:10:59 25 4
gpt4 key购买 nike

如果我想让通知在我按下 Android 模拟器上的 a 按钮时弹出一条消息“Hello”。我应该使用什么代码?再次感谢

最佳答案

这是当用户单击按钮时通知用户的示例代码

Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnclickListener(){
@Override
public void onClick(View v){

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.image,"Notification",System.currentTimeMillis());
Intent notificationIntent = new Intent(Currentclass.this,TargetActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), "Title", "Sub Title",contentIntent);
notification.flags=Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(143, notification);

}

});

这段代码可能对你有帮助

关于java - 在 android 模拟器上按下按钮时发出通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7443028/

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