本文概述
- 设置Android通知属性
- Android通知示例
设置Android通知属性 使用NotificationCompat.Builder对象设置Android通知的属性。下面提到了一些通知属性:
- setSmallIcon():设置通知图标。
- setContentTitle():用于设置通知标题。
- setContentText():用于设置文本消息。
- setAutoCancel():设置通知的cancelable属性。
- setPriority():设置通知的优先级。
activity_main.xml
【Kotlin Android通知】在activity_main.xml文件中添加以下代码。
<
?xml version="1.0" encoding="utf-8"?>
<
android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="example.srcmini.com.androidnotification.MainActivity">
<
TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="ANDROID NOTIFICATION"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintVertical_bias="0.091"android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>
<
Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button"android:layout_marginBottom="112dp"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:text="Notify"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent" />
<
/android.support.constraint.ConstraintLayout>
创建一个名为activity_notification_view.xml的活动, 并添加以下代码。该活动将在单击通知时启动。 TextView用于显示通知消息。
activity_notification_view.xml
<
?xml version="1.0" encoding="utf-8"?>
<
android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="example.srcmini.com.androidnotification.NotificationView">
<
TextViewandroid:id="@+id/textView2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center"android:text="your detail of notification..."android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />
<
TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="8dp"android:layout_marginEnd="8dp"android:layout_marginStart="8dp"android:layout_marginTop="8dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.096"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/textView2"app:layout_constraintVertical_bias="0.206"android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"/>
<
/android.support.constraint.ConstraintLayout>
MainActivity.java
在MainActivity.java类中添加以下代码。在此类中, 单击按钮将调用addNotification()方法, 在此方法中我们实现NotificationCompat.Builder对象以设置通知属性。 NotificationManager.notify()方法用于显示通知。 Intent类用于在拍打通知时调用另一个活动(NotificationView.java)。
package example.srcmini.com.androidnotification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {Button button;
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {addNotification();
}});
}private void addNotification() {NotificationCompat.Builder builder =new NotificationCompat.Builder(this).setSmallIcon(R.drawable.messageicon) //set icon for notification.setContentTitle("Notifications Example") //set title of notification.setContentText("This is a notification message")//this is notification message.setAutoCancel(true) // makes auto cancel of notification.setPriority(NotificationCompat.PRIORITY_DEFAULT);
//set priority of notificationIntent notificationIntent = new Intent(this, NotificationView.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//notification message will get at NotificationViewnotificationIntent.putExtra("message", "This is a notification message");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
// Add as notificationNotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}}
NotificationView.java
NotificationView.java类接收通知消息, 并显示在TextView中。点按通知时将调用此类。
package example.srcmini.com.androidnotification;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class NotificationView extends AppCompatActivity {TextView textView;
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_view);
textView = findViewById(R.id.textView);
//getting the notification messageString message=getIntent().getStringExtra("message");
textView.setText(message);
}}
strings.xml
<
resources>
<
string name="app_name">
AndroidNotification<
/string>
<
string name="notification_activity">
NotificationView<
/string>
<
/resources>
AndroidManifest.xml
在AndroidManifest.xml文件中添加以下代码。
<
?xml version="1.0" encoding="utf-8"?>
<
manifest xmlns:android="http://schemas.android.com/apk/res/android"package="example.srcmini.com.androidnotification">
<
applicationandroid: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=".NotificationView"android:label="@string/notification_activity"android:parentActivityName=".MainActivity">
<
meta-dataandroid:name="android.support.PARENT_ACTIVITY"android:value="http://www.srcmini.com/.MainActivity"/>
<
/activity>
<
/application>
<
/manifest>
输出:
文章图片
文章图片
文章图片
文章图片
推荐阅读
- Kotlin Android媒体播放器
- Kotlin Android Google Map当前位置
- Kotlin Android隐式Intent
- 使用URL的Kotlin Android JSON解析
- Kotlin Android Google Map固定位置
- Kotlin Android Google地图搜索位置
- Android Firebase身份验证-Google登录
- Kotlin Android Google AdMob横幅广告示例
- Kotlin Android Google AdMob非页内广告示例