第一行代码-8.2使用通知(两种改进方式)
程序员文章站
2022-03-28 19:09:17
郭神的第一行代码第二版通知部分如果继续按照源码编写已不可实现,特别是Android(8.0)以上版本需要增加渠道,在搜集诸多博客后整理出以下两种实现方式,话不多说直接上代码。方式一、package com.example.notice;import android.annotation.TargetApi;import android.app.Notification;import android.app.NotificationChannel;import android.app.Notif...
郭神的第一行代码第二版通知部分如果继续按照源码编写已不可实现,特别是Android(8.0)以上版本需要增加渠道,在搜集诸多博客后整理出以下两种实现方式,话不多说直接上代码。
方式一、
package com.example.notice;
import android.annotation.TargetApi;
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.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 {
private static final String TAG="MyService";
private static final String ID="channel_1";
private static final String NAME="前台服务";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.but);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendNotice();
}
});
}
@TargetApi(26)
private void sendNotice(){
Intent intent=new Intent(this,Main2Activity.class);
PendingIntent pi= PendingIntent.getActivity(this,0,intent,0);
NotificationManager notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel=new NotificationChannel (ID,NAME,NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel (channel);
Notification notification=new Notification.Builder(MainActivity.this,ID)
.setContentTitle ("收到一条重要通知")
.setContentText ("这是重要通知")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background))
.setContentIntent(pi)
.setAutoCancel(true)
.build();
notificationManager.notify(1,notification);
}
}
方式二、
package com.example.notificationtest;
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.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 implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendNotice = (Button) findViewById(R.id.btn);
sendNotice.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn:
Intent intent=new Intent(this, Main2Activity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){ //Android O (8.0)以上版本需要渠道
NotificationChannel notificationChannel = new NotificationChannel("channelid1","channelname",NotificationManager.IMPORTANCE_HIGH);//通知重要度,DEFAULT及以上,通知时手机默认会有振动
manager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"channelid1");
builder.setContentTitle("This is content title");
// builder.setContentText("This is content textThis is content textThis is content textThis is content textThis is content textThis is content textThis is content textThis is content text");
builder.setWhen(System.currentTimeMillis());
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
builder.setContentIntent(pendingIntent);
builder.setStyle(new NotificationCompat.BigTextStyle().bigText("666This is content textThis is content textThis is content textThis is content textThis is content textThis is content textThis is content textThis is content textThis is content text"));
builder.setPriority(NotificationCompat.PRIORITY_MIN);
builder.setAutoCancel(true);
manager.notify(1,builder.build());
break;
default:
break;
}
}
// /**
// * 播放通知声音
// */
// private static void playNotificationRing(Context context) {
// Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// Ringtone rt = RingtoneManager.getRingtone(context, uri);
// rt.play();
// }
//
// /**
// * 手机震动一下
// */
// private static void playNotificationVibrate(Context context) {
// Vibrator vibrator = (Vibrator) context.getSystemService(Service.VIBRATOR_SERVICE);
// long[] vibrationPattern = new long[]{0, 180, 80, 120};
// // 第一个参数为开关开关的时间,第二个参数是重复次数,振动需要添加权限
// vibrator.vibrate(vibrationPattern, -1);
// }
}
方式一和书中原文较为相似,只有少量修改,方式二差别较大。以上代码亲测有效,希望能够给你提供帮助。
参考链接
https://blog.csdn.net/chen_xiaoke/article/details/96113626
https://blog.csdn.net/w464960660/article/details/100012374
表示感谢!
本文地址:https://blog.csdn.net/weixin_45401735/article/details/107120542
上一篇: select下拉框选择字体大小
下一篇: C语言经典例题:猴子选大王