[√]android创建通知栏

简介: [√]android创建通知栏

自己实现的2中方式显示通知栏消息

方式1

 private void showNotify1() {
    Context context = this;
    final int notifyID = 1;
    final String CHANNEL_ID = "channel";
    // 创建一个Intent,指定启动的Activity
    Intent intent = new Intent(context, AppActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    // 创建一个PendingIntent,用于启动应用程序的Activity
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // 发送通知
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//      NotificationManager notificationManager = (NotificationManager) context.getSystemService(NotificationManager.class);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "default", NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);
        // 创建一个Notification,并设置其点击行为为启动Activity
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("title")
                .setContentText("text")
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .setAutoCancel(false);
        notificationManager.notify(notifyID, builder.build());
    }
}
  • Context.NOTIFICATION_SERVICE :notification
  • NotificationManager.class: class android.app.NotificationManager

方式2

private void showNotify() {
  String channel = "notify channel";
  final int notifyID = 1;
  String key = "click";
  Context context = this;
  Toast.makeText(context, "test toast", Toast.LENGTH_LONG).show();
  if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
      final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.notify);
      NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
      final Notification.Builder builder = new Notification.Builder(context, channel);
      builder.setAutoCancel(true);
      BroadcastReceiver buttonReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context ctx, Intent intent) {
//                    Intent intent1=new Intent(ctx,AppActivity.class);
//                    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//                    ctx.startActivity(intent1);
              String name = ctx.getPackageName();
              Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(name);
              if (launchIntent != null) {
                  launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                  launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  ctx.startActivity(launchIntent);
              }
          }
      };
      IntentFilter intentFilter = new IntentFilter();
      intentFilter.addAction(key);
      context.registerReceiver(buttonReceiver, intentFilter);
      views.setOnClickPendingIntent(R.id.btn, PendingIntent.getBroadcast(context, 0, new Intent(key), 0));
      builder.setOngoing(true)
              .setWhen(System.currentTimeMillis())
              .setSmallIcon(R.mipmap.ic_launcher)
              .setCustomContentView(views);
      @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(channel, "Default Channel", NotificationManager.IMPORTANCE_DEFAULT);
      notificationManager.createNotificationChannel(notificationChannel);
      notificationManager.notify(notifyID, builder.build());
  }
}

在 Android 中,可以使用 Intent 配合通知栏来创建交互式通知。

通过点击通知栏上的通知,可以触发特定的操作或打开相应的活动。

intent

在 Android 开发中,Intent 是一种用于在组件之间传递消息和执行操作的对象。它可以用于启动活动(Activity)、启动服务(Service)、发送广播(Broadcast)以及执行其他各种系统或应用组件之间的通信。

Intent 可以包含以下信息:

  1. Action(动作):指定要执行的操作,比如启动活动、启动服务等。例如,ACTION_VIEW 表示查看某个内容,ACTION_SEND 表示发送某个内容等。
  2. Data(数据):用于指定操作涉及的数据或内容。可以是一个 URI、一个文件路径等。
  3. Extras(附加数据):可选的键值对,用于传递额外的数据给目标组件。可以通过 putExtra() 方法添加多个键值对。
  4. Category(类别):用于进一步指定操作的类别。

使用 Intent 的基本流程如下:

  1. 创建一个新的 Intent 对象,指定要执行的操作类型(Action)和相关参数。
  2. 可选的设置其他属性,如数据(Data)、类别(Category)等。
  3. 根据需求,添加附加数据(Extras)。
  4. 调用 startActivity() 方法启动活动,startService() 方法启动服务,sendBroadcast() 方法发送广播等,将 Intent 发送给目标组件。

以下是一个简单的示例,演示如何使用 Intent 启动一个活动:

// 创建一个新的 Intent 对象
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
// 可选:设置数据
intent.setData(Uri.parse("https://www.example.com"));
// 可选:添加附加数据
intent.putExtra("key", "value");
// 启动活动
startActivity(intent);

在上面的示例中,我们创建了一个新的 Intent 对象,并指定了要启动的目标活动为 SecondActivity。我们还通过 setData() 方法设置了数据为一个网址,并通过 putExtra() 方法添加了一个键值对。最后,我们使用 startActivity() 方法将 Intent 发送给系统来启动目标活动。

通过 Intent,我们可以实现在不同的组件之间进行通信和交互,从而实现丰富的 Android 应用程序功能。

目录
相关文章
|
2月前
|
Java Android开发
Android 开发获取通知栏权限时会出现两个应用图标
Android 开发获取通知栏权限时会出现两个应用图标
14 0
|
9月前
|
Android开发 UED
Android 实现通知栏和进度条效果(适用于Android8.0以上)
Android 实现通知栏和进度条效果(适用于Android8.0以上)
110 0
|
6月前
|
API Android开发
[√]Android 通知栏
[√]Android 通知栏
26 0
|
10月前
|
Android开发
Android中下拉通知栏,Activity会走哪些生命周期?
我们就可以做一个总结:当前Activity中,下拉通知栏,是不走任何生命周期的。
155 0
|
Android开发
Android 音乐APP(五)音乐通知栏、后台播放音乐
Android 音乐APP(五)音乐通知栏、后台播放音乐
1031 0
Android 音乐APP(五)音乐通知栏、后台播放音乐
|
XML SQL 缓存
Android 音乐APP(四)显示专辑图片、本地数据库、自定义通知栏样式、通知栏显示
Android 音乐APP(四)显示专辑图片、本地数据库、自定义通知栏样式、通知栏显示
399 0
Android 音乐APP(四)显示专辑图片、本地数据库、自定义通知栏样式、通知栏显示
|
存储 XML Java
Android 天气APP(二十六)增加自动更新(检查版本、通知栏下载、自动安装)
Android 天气APP(二十六)增加自动更新(检查版本、通知栏下载、自动安装)
173 0
Android 天气APP(二十六)增加自动更新(检查版本、通知栏下载、自动安装)
|
Android开发
Android开发禁用通知栏下拉
Android开发禁用通知栏下拉
235 0
|
Android开发
Android 点击通知栏消息打开activity,并判断app是否运行
Android 点击通知栏消息打开activity,并判断app是否运行
1691 0