所以我正在研究一个简单的音乐播放器.音乐播放器命名状态,可以播放歌曲,暂停播放,转发到下一首歌曲,返回上一首歌曲,并完全停止播放.播放歌曲时,会显示艺术家姓名和歌曲名称的通知; 此通知还有三个按钮(操作):停止,暂停下一个.
我遇到的问题是确保在单击任一操作时,触发与Action相关的播放控件,而且我完全不知道如何执行此操作.我搜索了Android通知:http://developer.android.com/guide/topics/ui/notifiers/notifications.html但它没有澄清,也没有提供太多关于通知操作的信息.
这是一个简单的操作(例如,应该与"下一步"按钮相关联点击通知:注意:下面描述的所有代码都是在以下包中编写的:com.deadpixels.light.player并且调用了Activity :PlayerActivity
public void nextSong() {
if (position == songs.size() -1) {
Toast.makeText(this, "No more songs on queue", Toast.LENGTH_SHORT).show();
if(mPlayer.isPlaying()) {
return;
}
else {
buttonPlay.setImageResource(R.drawable.button_play);
}
return;
}
else {
position++;
playSong(songs.get(position));
}
}
Run Code Online (Sandbox Code Playgroud)
这是我试图做的:
Intent nextIntent = new Intent(KEY_NEXT);
PendingIntent nextPendingIntent = createPendingResult(0, nextIntent, 0);
Run Code Online (Sandbox Code Playgroud)
NextIntent的操作是:
public static final String KEY_NEXT = "com.deadpixels.light.player.PlayerActivity.nextSong";
Run Code Online (Sandbox Code Playgroud)
然后我通过addAction()将其添加到通知中:
mBuilder.addAction(R.drawable.not_action_next, "Next", nextPendingIntent);
Run Code Online (Sandbox Code Playgroud)
你们知道我还能尝试什么吗?使用我上面解释的内容什么都没做,通知显示,并有三个操作按钮,但点击它们对我没有任何作用.
有一次,我想可能是我添加了带有动作名称的intent过滤器,但后来我想,好吧,它们都在同一名称空间中,我为什么要这样做?
android mediaplayback android-notifications android-pendingintent
我正在开发一个基于通知的应用程序,我需要收听传入的通知.我已经能够收听来电,短信,邮件等.我不知道如何通过代码在Whatsapp上听朋友的ping或消息.这可以实际完成吗?如果是这样,怎么样?可以使用Accessibility Service,使用Package Name作为"com.whatsapp"吗?
android android-intent android-notifications android-notification-bar android-broadcast
为什么我们TaskStackBuilder在创建通知时使用?我没有得到它背后的逻辑.
有人可以解释一下.
public void showText(final String text){
Intent intent = new Intent (this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_MAX)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentIntent(pendingIntent)
.setContentText(text)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICACTION_ID, notification);
}
Run Code Online (Sandbox Code Playgroud) 我基本上只是尝试Android开发,几天前我遇到了这个名为" Go SMS Pro "的应用程序,除其他外,它可以设置不同颜色的通知(蓝色,绿色,橙色,粉红色和浅色)蓝色).所以,我已经尝试在我自己的应用程序中自己做这件事,但是我不能改变颜色和LED的内部闪烁.我目前使用此代码:
public class MainActivity extends Activity {
static final int NOTIFICATION_ID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(buttonOnClick);
}
public OnClickListener buttonOnClick = new OnClickListener() {
@Override
public void onClick(View v) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Notification notification = new Notification(R.drawable.icon, "Hello", System.currentTimeMillis());
notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;
notification.ledARGB = Color.BLUE;
notification.ledOnMS = 1000;
notification.ledOffMS = 300;
Context context = getApplicationContext();
CharSequence …Run Code Online (Sandbox Code Playgroud) 如何在android中设置播放/暂停,下一个和上一个按钮的通知.
我是Android的新手,也是堆栈溢出的新手.所以请耐心等待.

我开始播放歌曲时设置通知,如下所示:
`
@SuppressLint("NewApi")
public void setNotification(String songName){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
@SuppressWarnings("deprecation")
Notification notification = new Notification(R.drawable.god_img, null, System.currentTimeMillis());
RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification_mediacontroller);
//the intent that is started when the notification is clicked (works)
Intent notificationIntent = new Intent(this, AudioBookListActivity.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentView = notificationView;
notification.contentIntent = pendingNotificationIntent;
notification.flags |= Notification.FLAG_NO_CLEAR;
//this is the intent that is supposed to be called when the button is clicked …Run Code Online (Sandbox Code Playgroud) android android-notifications android-mediaplayer android-broadcast
在Android设置中,如果用户不愿意,可以关闭通知.那么有什么方法isNotificationAllowed()可以检查我的应用是否允许显示通知?以及如何打开android设置页面来引导我的用户打开通知?
我想继续通知我ForegroundService需要尽可能小的地方.我喜欢"Android系统 - USB充电这个设备"的风格,但我找不到任何如何实现这个的例子.

谁能指出我正确的方向?
如果为频道分配了重要性,则会为通知指定样式IMPORTANCE_MIN.
它看起来像有没有办法使用内置样式的通知机器人会IMPORTANCE_MIN与被用ForegroundService.
以下是对的描述IMPORTANCE_MIN:
最小通知重要性:仅在阴影下方显示.这不应该与Service.startForeground一起使用,因为前台服务应该是用户关心的东西,因此将其通知标记为最低重要性并不具有语义意义.如果您从Android版本Build.VERSION_CODES.O开始执行此操作,系统将显示有关您的应用在后台运行的更高优先级通知.
当用户按下Send "Button 1"(向下滚动以查看应用程序的构造)时,将Notification创建一个新的RefreshService.如果用户按下此通知,则MainActivity实例启动并接收String具有Button 1超过该值的值Intent.
显示该值.
当用户现在按下时,Send "Button 2"将Notification创建一个新的RefreshService.如果用户按下此通知,则会MainActivity启动一个实例,并接收一个ALSO值超过该值的ALSO.String Button 1Intent
所以你可以猜测,通常应该有价值Button 2.
当用户按下的第一个按钮时,Send "Button 2"总会Button 2发送.
获得第二个按钮值的唯一解决方案是重新启动手机并先按下第二个按钮.即使强行关闭也行不通.
我知道我也可以用另一种方式更改UI.但是我需要在应用程序中使用这种方法,我需要用另一个重新启动'MainActivity',Intent因此方法应该是相同的.
一个Activity叫MainActivity
一个IntentService叫RefreshService
主要活动
public class MainActivity extends Activity implements View.OnClickListener {
public static final String RECEIVED = "received"; …Run Code Online (Sandbox Code Playgroud) android android-intent android-notifications intentservice android-activity
使用NotificationManagerCompat取消所有通知.
NotificationManagerCompat manager =
NotificationManagerCompat.from(ctx.getApplicationContext());
manager.cancelAll();
Run Code Online (Sandbox Code Playgroud)
它有一段时间异常(大部分时间都有效).
在Andoid 6上:
java.lang.SecurityException:Permission Denial:来自pid = 22994的getCurrentUser(),uid = 10184需要android.permission.INTERACT_ACROSS_USERS
Fatal Exception: java.lang.SecurityException: Permission Denial: getCurrentUser() from pid=22994, uid=10184 requires android.permission.INTERACT_ACROSS_USERS
at android.os.Parcel.readException(Parcel.java:1602)
at android.os.Parcel.readException(Parcel.java:1555)
at android.app.INotificationManager$Stub$Proxy.cancelAllNotifications(INotificationManager.java:649)
at android.app.NotificationManager.cancelAll(NotificationManager.java:323)
at android.support.v4.app.NotificationManagerCompat.cancelAll(NotificationManagerCompat.java:197)
Run Code Online (Sandbox Code Playgroud)
在Android 5.0,4.4.2上:
ava.lang.SecurityException:权限拒绝:来自pid = 5460的getIntentSender(),uid = 10135,(需要uid = 1000)不允许在android.os.Parcel.readException(Parcel.java:1465)中作为包android发送
Fatal Exception: java.lang.SecurityException: Permission Denial: getIntentSender() from pid=3109, uid=10153, (need uid=1000) is not allowed to send as package android
at android.os.Parcel.readException(Parcel.java:1472)
at android.os.Parcel.readException(Parcel.java:1426)
at android.app.INotificationManager$Stub$Proxy.cancelAllNotifications(INotificationManager.java:271)
at android.app.NotificationManager.cancelAll(NotificationManager.java:220)
at android.support.v4.app.NotificationManagerCompat.cancelAll(NotificationManagerCompat.java:197)
Run Code Online (Sandbox Code Playgroud)
问题:
ctx.getApplicationContext().getApplicationInfo().uid …android android-notifications android-securityexception android-binder
android ×10