Android 通知的使用

(1)使用系统定义的Notification

MainActivity.class

package com.example.test;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity implements OnClickListener{
 
 private Button btn_notification;

 protected void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.activity_main);
  initViews();
  initListener();
 }
 
 private void initViews(){
  btn_notification = (Button) findViewById(R.id.btn_notification);
  
 }
 
 private void initListener(){
  btn_notification.setOnClickListener(this);
 }
 
 @SuppressWarnings("deprecation")
 @SuppressLint("NewApi")
 public void onClick(View v){
  int id = v.getId();
  if(id == R.id.btn_notification){

   // 新版本的通知的使用
   NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
   PendingIntent pendingIntent = PendingIntent.getActivity(
     getApplicationContext(),
     0,
     new Intent(getApplicationContext(),NotificationJumpActivity.class),
     0);
   Builder builder = new Notification.Builder(getApplicationContext());
   builder.setContentTitle("通知的标题"); // 设置下拉列表里面的标题
   builder.setContentText("通知的内容");  // 设置上下文内容
   builder.setWhen(System.currentTimeMillis()); // 设置时间
   builder.setAutoCancel(true); // 设置可以清除

   // 设置状态栏里通知的图标,不知道是不是手机问题,当把这句注释掉时,能收到通知,但是在状态栏里看不到这条通知,在下拉列表中也不会看到这条通知。
   builder.setSmallIcon(this.getApplicationInfo().icon);

   // 设置 下拉消息时,消息左边的图标,这句注释掉的话呢,通知栏里会有,下拉里面就没有了哦
   builder.setLargeIcon(BitmapFactory.decodeResource(this.getApplicationContext().getResources(), R.drawable.ic_launcher)); 
   builder.setTicker("这是状态栏的显示");
   builder.setContentIntent(pendingIntent);
   
   
   @SuppressWarnings("deprecation")
   Notification notification = builder.getNotification();
   // Notification notification = builder.build();// API 16 以上使用 即 Android 4.1.2版本
   notification.defaults = Notification.DEFAULT_SOUND;  // 默认声音
   notification.flags = Notification.FLAG_AUTO_CANCEL;  // 自动终止
   notification.contentIntent = pendingIntent;
   nm.notify(0, notification);
   
   
   
//   // 旧版本的通知的使用
//   NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//   Notification notification = new Notification(R.drawable.ic_launcher,"我的通知",System.currentTimeMillis());
//   notification.defaults = Notification.DEFAULT_SOUND;
//   notification.tickerText = "这是状态栏通知内容";
//   notification.icon = R.drawable.ic_launcher;
//   notification.setLatestEventInfo(getApplicationContext(), "我是标题", "我是内容",
//     PendingIntent.getActivity(getApplicationContext(), 0, new Intent(MainActivity.this,NotificationJumpActivity.class), 0));
//   nm.notify(0, notification);
   
  }
 }

}

 

 

activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
    <Button
        android:id="@+id/btn_notification"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text_notification"
        />

</LinearLayout>

 

 

NotificationJumpActivity.class

 

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;

public class NotificationJumpActivity extends Activity{
 
 private TextView tv_content;
 
 protected void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.activity_jump);
  
  initViews();
 }
 
 private void initViews(){
  tv_content = (TextView) findViewById(R.id.tv_content);
  tv_content.setText(R.string.jump_tv_content);
 }

}

 

activity_jump.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
    <TextView
        android:id="@+id/tv_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

 

strings.xml

<resources>

    <string name="app_name">Test</string>
   
    <string name="text_notification">通知</string>
    <string name="jump_tv_content">这是从通知状态栏跳转过来打开的Activity</string>

</resources>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Android Studio 创建通知的示例代码: 1. 在 `AndroidManifest.xml` 文件中添加通知权限: ```xml <uses-permission android:name="android.permission.VIBRATE"/> ``` 2. 在布局文件中创建一个按钮并设置其 `onClick` 事件: ```xml <Button android:id="@+id/notification_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Notification" android:onClick="sendNotification"/> ``` 3. 在 Activity 中实现 `sendNotification` 方法,该方法创建并发送通知: ```java public void sendNotification(View view) { // 创建通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "my_channel") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT); // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(0, builder.build()); } ``` 上述代码中,我们创建了一个包含小图标、标题和内容的通知,并将其发送到系统通知栏中。注意,我们需要指定一个通知渠道,这可以在应用启动时进行设置。 4. 在应用启动时创建通知渠道: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 创建通知渠道 CharSequence name = getString(R.string.channel_name); String description = getString(R.string.channel_description); int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel("my_channel", name, importance); channel.setDescription(description); // 注册通知渠道 NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } ``` 上述代码中,我们检查当前 Android 版本是否支持通知渠道,如果支持,则创建一个默认级别的通知渠道,并在系统中注册该渠道。 现在,当用户点击按钮时,应用将创建并发送一个通知到系统通知栏中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值