Android通知
16lz
2022-05-16
通知能提醒我们。Android提供了强大的通知功能,现在创建一个简单的通知测试项目:
NotificationTest
添加依赖
通知需要依赖support-compat,以后可能更多的使用AndroidX。
dependencies { implementation "com.android.support:support-compat:28.0.0"}
创建活动
要创建两个活动,一个是主活动,一个是结果活动,用来显示点击通知后的结果页。
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.notificationtest"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> intent-filter> activity> <activity android:name=".ResultActivity" android:launchMode="singleTask" android:taskAffinity="" android:excludeFromRecents="true" android:parentActivityName=".MainActivity"> activity> application>manifest>
布局activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/send_notice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发送通知"/>LinearLayout>
布局activity_result.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="24sp" android:text="结果页"/>RelativeLayout>
MainActivity
package com.example.notificationtest;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.os.Build;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button sendNotice = findViewById(R.id.send_notice); sendNotice.setOnClickListener(this); } @Override public void onClick(View view) { final String CHANNEL_ID = "1"; switch (view.getId()) { case R.id.send_notice: Log.d("test", "测试"); // 创建通知“意图” Intent notifyIntent = new Intent(this, ResultActivity.class); notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent notifyPendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, "默认通知", NotificationManager.IMPORTANCE_HIGH); manager.createNotificationChannel(notificationChannel); } Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) .setDefaults(Notification.DEFAULT_ALL) .setContentTitle("这是内容标题") .setContentText("这是正文内容") .setWhen(System.currentTimeMillis()) // 指定被通知的时间 .setSmallIcon(R.mipmap.ic_launcher) // 设置小图标 .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.xiong)) .setAutoCancel(true) // 设置自动隐藏 .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setContentIntent(notifyPendingIntent) // 设置意图 .build(); manager.notify(1, notification); break; default: break; } }}
ResultActivity
package com.example.notificationtest;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class ResultActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_result); }}
参考:
https://developer.android.google.cn/guide/topics/ui/notifiers/notifications
更多相关文章
- ubuntu 64位 无法安装android SDK解决办法
- Android监听系统通知
- Android(安卓)AES加密算法及其实现
- Android(安卓)中Notification的运用
- android Intent在活动之间穿梭
- android app应用内更新
- Android消息通知
- Android(安卓)启动另外activity并返回结果
- Android中的通知—Notification