您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
76_notification的使用
发布时间:2021-03-16 23:21:39编辑:雪饮阅读()
这次要实现的是Notification,是安卓上应用程序向通知栏发送通知的功能。
<androidx.constraintlayout.widget.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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示notification"
android:onClick="click"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
至于具体的实现逻辑,从安卓2系列开始安卓发展迅猛,sdk日新月异,api也是变化万千。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notifaction">
<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/Theme.Notifaction">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java:
package com.example.notifaction;
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.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import java.io.File;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
String channelId="testChannel";
//建立频道
NotificationChannel channel=new NotificationChannel(channelId,"testChannelName",NotificationManager.IMPORTANCE_DEFAULT);
Uri mUri = Uri.fromFile(new File("/sdcard/cl.mp3"));
channel.setSound(mUri, Notification.AUDIO_ATTRIBUTES_DEFAULT);
//用频道建立通知对象
Notification.Builder builder = new Notification.Builder(MainActivity.this,channelId);
builder.setContentTitle("notife 标题");
builder.setContentText("noti 正文");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker("tickertext");
builder.setWhen(System.currentTimeMillis());
Intent intent = new Intent(this,MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(contentIntent);
Notification notification = builder.build();//将builder对象转换为普通的notification
notification.flags=Notification.FLAG_NO_CLEAR;
manager.createNotificationChannel(channel);
manager.notify(1, notification);
}
}这里有一个必须注意的事项,就是当你代码中更换了音乐后,你要在设备中将该应用卸载,然后重新部署最新代码才可以,否则还是读取的原来的音乐。
另外就是api28中这个频道与通知之间的关联,从上面可以看出实在是有点多余。
如建立频道指定了频道id:
NotificationChannel channel=new NotificationChannel(channelId,"testChannelName",NotificationManager.IMPORTANCE_DEFAULT);
建立builder指定了频道id
Notification.Builder builder = new Notification.Builder(MainActivity.this,channelId);
最后通知管理又指定了一个频道
manager.createNotificationChannel(channel);
其实按我说,在builder构造时候既然已经设置了频道id,而builder最后会转换为notification,那么通知管理最后执行通知的也是这个notification,那么又何必多次一举,我虽然尝试过只留一个,然而发现不行,我尝试的是将构造的时候不传频道id,但是通知管理创建通知频道我是创建了的。结果不行。
于是我又测试不用通知管理创建频道,仅仅在builder构造时候传入频道id,结果也不行。
好吧。。。。看看大家有什么办法咯。我反正是没有办法咯。
话不多说,咱们来看效果
这第一次我点击通知之后可以看到通知栏有个小圆形白点,这是我第一次用的通知栏图标
那么我之后修改了的通知图标就是这样了
对应的就是这个咯
那么最重要的是,第一次通知栏展开后是这样
第二次通知栏展开后是这样
会发现图标有两种状态,一个是通知栏没有展开的还有一个是通知栏展开后的在通知列表中的列表项中所呈现的样式。由于我分别测试了两种不同的图标文件,所以总体下来就出现了四种样式。。。
那么这里音乐也会响起的,文字是无法描述的。。。
还有一个共同点就是,看不到那个清理通知栏的一般位于屏幕最右侧的那个按钮了。这也就是我们程序中
notification.flags=Notification.FLAG_NO_CLEAR;
所起到的“功劳”,虽然我没有尝试过去除这个代码之后再看看效果,不过十之八九咯。
今天就先到这里了,有点累了,开发这东西还真是很折腾呢。
关键字词:android,notification,通知栏