[英]Stop Notification sound when Notification clicked android
I am building an android application which show notification with custom sound, when user click this notification I start an activity.我正在构建一个 android 应用程序,它显示带有自定义声音的通知,当用户单击此通知时,我开始一个活动。 all of that working well for me but when user clicked notification sound still playing I want to stop the sound when user clicked on notification How can I do that.所有这些对我来说都很好,但是当用户单击通知声音时仍在播放我想在用户单击通知时停止声音我该怎么做。 this is my code这是我的代码
public void ShowNotification(String title,String message) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("com.mkandeel.test","Notification",
NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = context.getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"com.mkandeel.test");
builder.setSmallIcon(R.drawable.notif)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH);
Intent intent = new Intent(context,MainActivity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pi = PendingIntent.getActivity(context,99,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pi);
NotificationManagerCompat mcompact = NotificationManagerCompat.from(context);
Uri sound = Uri.parse("android.resource://"
+ context.getPackageName() + "/" + R.raw.sound);
MediaPlayer mp = MediaPlayer.create(context, sound);
mp.start();
builder.setAutoCancel(true);
mcompact.notify(99,builder.build());
}
I finally found solution to create MediaPlayer object global variable and send boolean extra in intent as true and in MainActivity2 I got this boolean variable and if it is true I accessed MediaPlayer object and stop it and this is modified code我终于找到了创建 MediaPlayer 对象全局变量的解决方案,并以 true 和 MainActivity2 的形式发送额外的布尔值,我得到了这个布尔变量,如果它是 true,我访问了 MediaPlayer 对象并停止它,这是修改后的代码
public class MainActivity extends AppCompatActivity {
public MediaPlayer mp;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ShowNotification("Test notification","Hello World!");
}
});
}
public void ShowNotification(String title,String message) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("com.mkandeel.test","Notification",
NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = context.getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"com.mkandeel.test");
builder.setSmallIcon(R.drawable.notif)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH);
Intent intent = new Intent(context,MainActivity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.putExtra("clicked",true);
PendingIntent pi = PendingIntent.getActivity(context,99,
intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pi);
NotificationManagerCompat mcompact = NotificationManagerCompat.from(context);
Uri sound = Uri.parse("android.resource://"
+ context.getPackageName() + "/" + R.raw.sound);
mp = MediaPlayer.create(context, sound);
mp.start();
builder.setAutoCancel(true);
mcompact.notify(99,builder.build());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.