我正在尝试在Android中实现通知.
现在我有一个问题,我不希望有一个PendingIntent用户会打开任何Activity.我怎样才能做到这一点?
我需要创建多个状态栏通知.当我拉下状态栏时,应该将多个通知图标显示为列表.每个通知图标都应显示下一页显示的单独数据.我怎么能这样做?
我的代码:
public class SimpleNotification extends Activity {
private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;
String str="Hai";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
final Notification notifyDetails = new Notification(R.drawable.android,"New Alert, Click Me!",System.currentTimeMillis());
Button start = (Button)findViewById(R.id.notifyButton);
Button cancel = (Button)findViewById(R.id.cancelButton);
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Context context = getApplicationContext();
CharSequence contentTitle = "Notification Details...";
CharSequence contentText = "Browse Android Official Site by clicking me"; …Run Code Online (Sandbox Code Playgroud) 我想创建一个包含一些控件的通知.由于文本和控件很小,默认通知大小(64dp),我希望它大于默认大小.
可以创建更大的通知,我认为也可以有自定义布局,但我不知道如何.
更具体地说,以下屏幕截图显示了spotify的通知(图片取自此处):
如您所见,尺寸大于默认值.此外,它有一些没有文本的ImageButtons - 如果你使用Notification.Builder.addAction(),你可以提供一个图标,但也需要提供一个CharSequence作为描述 - 如果你把描述留空,仍然会有空间为文本保留,如果传递null,它将崩溃.
任何人都可以告诉我如何使用自定义布局创建大型通知吗?
谢谢
在Android N使用分屏时,我希望activity在用户点击通知时在当前活动窗口中启动,但如果通过单击通知启动,则Android N始终activity在第二个窗口中启动.
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification)
.setAutoCancel(false)
.setContentTitle("Demo Title")
.setContentText("Demo");
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("myIntent", "test");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(156, notification);
Run Code Online (Sandbox Code Playgroud)
什么时候intent比发射activity.
Intent in = new Intent(MainActivity.this, SecondActivity.class);
startActivity(in);
Run Code Online (Sandbox Code Playgroud)
前 - 我在前台有两个应用程序,如第一个窗口中的Chrome浏览器和第二个窗口中的Facebook,现在我在Chrome浏览器中搜索某些内容,此时我收到了通知Gmail.现在当我点击Gmail通知而不是通过替换Facebook在第二个窗口中 …
android android-intent android-notifications split-screen android-7.0-nougat
如何在构建时发出不发出声音的通知?我正在建立一个通知,我的用户不喜欢它发出声音的事实.
如何将其更改为静音/无声?
我如何显示通知:
android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(main);
builder.setStyle(new android.support.v7.app.NotificationCompat.BigTextStyle().bigText(text));
builder.setSmallIcon(R.drawable.app);
builder.setContentTitle("Rooster Maandag:");
builder.setOngoing(false);
builder.setAutoCancel(true);
builder.setDefaults(Notification.DEFAULT_ALL);
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
notificationManager = (NotificationManager) main.getSystemService(main.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Run Code Online (Sandbox Code Playgroud)
我试图在谷歌搜索,但我得到的唯一结果是如何播放声音,而不是如何播放声音...
编辑 它可能在某些人的眼中是重复的,但在我的中我找不到指定默认值的替代方法,而这个新方法称为setDefaults
InflationException当我ConstraintLayout在自定义中使用a时,我得到了一个Notification.我可以在我的应用程序的其他地方使用它们,而不是在自定义布局中Notification.我假设支持库中包含的任何小部件根本无法在a中使用RemoteView,但我找不到任何确认.有人知道吗?
E/StatusBar: couldn't inflate view for notification com.example.app/0x1
android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.constraint.ConstraintLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.constraint.ConstraintLayout" on path: DexPathList[[],nativeLibraryDirectories=[/system/priv-app/TeleService/lib/arm64, /system/fake-libs64, /system/priv-app/TeleService/TeleService.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64, /system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.view.LayoutInflater.createView(LayoutInflater.java:606)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.widget.RemoteViews.inflateView(RemoteViews.java:3278)
at android.widget.RemoteViews.-wrap1(Unknown …Run Code Online (Sandbox Code Playgroud) 我有以下代码,但每次我只是听到默认的Android声音.
// create channel
NotificationChannel channel = new NotificationChannel(ANDROID_CHANNEL_ID,
ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
// Sets whether notifications posted to this channel should display notification lights
channel.enableLights(true);
// Sets whether notification posted to this channel should vibrate.
channel.enableVibration(true);
// Sets the notification light color for notifications posted to this channel
channel.setLightColor(Color.GREEN);
// Sets whether notifications posted to this channel appear on the lockscreen or not
//channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
Uri uri = Uri.parse("android.resource://"+this.getPackageName()+"/" + R.raw.aperturaabductores);
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
channel.setSound(uri,att);
Run Code Online (Sandbox Code Playgroud)
这是我的声音 …
我正在创建一个前台服务,其中包含服务启动时通知栏中显示的通知.如果服务停止,通知将解除.我的问题是,当"清除所有通知"或解除通知(滑动)时,有没有办法停止服务?
更新以包括通知的实施:
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.d(CLASS, "onStartCommand service started.");
if (getString(R.string.service_start_action) == intent.getAction())
{
Intent intentForeground = new Intent(this, ServiceMain.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intentForeground, 0);
Notification notification;
Notification.Builder builder = new Notification.Builder(getApplicationContext())
.setSmallIcon(android.R.drawable.btn_star)
.setTicker("Service started...")
.setContentIntent(pendIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setOnlyAlertOnce(true)
.setOngoing(false);
notification = builder.build();
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
startForeground(SERV_NOTIFY, notification);
player.start();
}
else
{
Log.d(CLASS, "onStartCommand unable to identify acition.");
}
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud) 我有一个通知,支持播放,前进和后退.
private static Notification createNotification(String interpret, String title, boolean paused) {
// if (builder == null)
builder = new NotificationCompat.Builder(context);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setAutoCancel(false);
builder.setContentTitle(title);
builder.setContentText(interpret);
builder.setOngoing(true);
builder.setOnlyAlertOnce(true);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentIntent(PendingIntent.getActivity(context, 9, new Intent(context, ApplicationActivity.class), Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT));
builder.addAction(R.drawable.av_previous, "", PendingIntent.getBroadcast(context.getApplicationContext(), 0, new Intent(NotificationPlayerControlReceiver.MUSIC_PLAYER_INTENT).putExtra("resultcode", NotificationPlayerControlReceiver.PREVIOUS), PendingIntent.FLAG_CANCEL_CURRENT));
if (paused)
builder.addAction(R.drawable.av_play, "", PendingIntent.getBroadcast(context.getApplicationContext(), 2, new Intent(NotificationPlayerControlReceiver.MUSIC_PLAYER_INTENT).putExtra("resultcode", NotificationPlayerControlReceiver.PLAY), PendingIntent.FLAG_CANCEL_CURRENT));
else
builder.addAction(R.drawable.av_pause, "", PendingIntent.getBroadcast(context.getApplicationContext(), 3, new Intent(NotificationPlayerControlReceiver.MUSIC_PLAYER_INTENT).putExtra("resultcode", NotificationPlayerControlReceiver.PAUSE), PendingIntent.FLAG_CANCEL_CURRENT));
builder.addAction(R.drawable.av_next, "", PendingIntent.getBroadcast(context.getApplicationContext(), 1, new Intent(NotificationPlayerControlReceiver.MUSIC_PLAYER_INTENT).putExtra("resultcode", NotificationPlayerControlReceiver.NEXT), PendingIntent.FLAG_CANCEL_CURRENT));
Notification notification = builder.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
notification.tickerView = null;
return …Run Code Online (Sandbox Code Playgroud) 我正在使用兼容性库实现Android丰富的通知,所以我的所有通知都是使用构建的 android.support.v4.app.NotificationCompat.Builder
我正在使用的代码如下:
// Base notification
NotificationCompat.Builder b = new NotificationCompat.Builder(context);
b.setSmallIcon(R.drawable.ic_actionbar);
b.setContentTitle(title);
b.setContentText(Html.fromHtml(msg));
b.setTicker(title);
b.setWhen(System.currentTimeMillis());
b.setDeleteIntent(getDismissNotificationsPendingIntent(quantity));
b.setLargeIcon(Picasso.with(context).load(iconUrl).get());
// BigPictureStyle
NotificationCompat.BigPictureStyle s = new NotificationCompat.BigPictureStyle();
if (expandedIconUrl != null) {
s.bigLargeIcon(Picasso.with(context).load(expandedIconUrl).get());
} else if (expandedIconResId > 0) {
s.bigLargeIcon(BitmapFactory.decodeResource(context.getResources(), expandedIconResId));
}
s.bigPicture(Picasso.with(context).load(bigImageUrl).get());
b.setStyle(s);
b.setContentIntent( // some intent
b.addAction(R.drawable.ic_notification_ // some action
Notification n = b.build();
// and go ahead to show it
Run Code Online (Sandbox Code Playgroud)
如果显示图像不兼容,基本上没有加载任何图像,所以我们不会无缘无故地使用内存,但这是基础,我期待类似于下图中右边的通知

问题是消息(在示例"触摸以查看屏幕截图.")显示通知何时签约,但是当通知扩展时,消息消失.
有什么setMessage()方法我忘记打电话了吗?这是一个错误NotificationCompat吗?谁能在这里提供一些见解?