Notification
Notification
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendNotification" android:text="发送通知"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="cancelNotification" android:text="关闭通知"/>
点击通知跳转到的页面
package com.example.mynotification; import android.app.Activity; import android.os.Bundle; import android.util.Log; import androidx.annotation.Nullable; public class NotificationActivity extends Activity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e("wmj", "NotificationActivity"); } }
主页面
package com.example.mynotification; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.NotificationCompat; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Intent; import android.graphics.BitmapFactory; import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { private NotificationManager notificationManager; private Notification notification; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取通知管理器类对象 notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); // 判断版本是否是8.0及以上 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ // 通知渠道,参数3是通知重要程度 NotificationChannel notificationChannel = new NotificationChannel("wmj", "测试通知", NotificationManager.IMPORTANCE_HIGH); notificationManager.createNotificationChannel(notificationChannel); } // 点击通知后跳转到的页面 Intent intent = new Intent(this, NotificationActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); // Builder构造器创建Notification对象 notification = new NotificationCompat.Builder(this, "wmj") .setContentTitle("重要通知") .setContentText("哈哈哈") .setAutoCancel(true) // 点击通知后自动消失 .setSmallIcon(R.drawable.ic_baseline_person_24) // 不能使用彩色图片 .setContentIntent(pendingIntent) // 设置待定的页面 .setColor(Color.parseColor("#FF0000")) // 小图标颜色 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.dog)) // 通知右侧大图标 .build();// id和上面一致 } public void sendNotification(View view) { notificationManager.notify(1, notification); } public void cancelNotification(View view) { notificationManager.cancel(1); } }
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/15379501.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步