下面的代码适用于平板电脑,但不适用于Android手机。我想显示通知。
我的代码是:
if(trigId.equals("0")){
intent=new Intent(this,message_detail.class);
}else{
intent=new Intent(this,appinement_list.class);
}
PendingIntent pIntent=PendingIntent.getActivity(this, 0, intent, 0);
// nt=new Notification(R.drawable.ic_launcher, "Welco0me", System.currentTimeMillis());
Notification noti = new Notification.Builder(this)
.setContentTitle("Notification From EHIQ").setSound(alarmSound).setContentIntent(pIntent)
.setContentText(" "+Helper_MSGNotification.Subject.get(i)).setSmallIcon(aspl.scorehealth.R.drawable.ic_launcher).build();
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
noti.flags|=Notification.FLAG_AUTO_CANCEL;
manager.notify(i,noti);
我很困惑实际上有什么问题,请帮助我。
答案 0 :(得分:1)
NotificationCompat.Builder
是在所有Android版本上创建Notifications
的最简单方法。您甚至可以使用Android 4.1提供的功能。如果您的应用在Android> = 4.1的设备上运行,则会使用新功能,如果在Android< 4.1上运行,通知将是一个简单的旧通知。
使用Notification.Builder
和NotificationCompat.Builder
简单替换import android.support.v4.app.NotificationCompat
。
要创建简单的通知,请执行以下操作:(请参阅Android API Guide on Notifications)
NotificationCompat.Builder myNotification = new NotificationCompat.Builder(context)
.setContentTitle("Exercise of Notification!")
.setContentText("http://android-er.blogspot.com/")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
的更多信息
答案 1 :(得分:0)
尝试这个,
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(getApplicationContext(), MYDEMOACTIVITY.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MYDEMOACTIVITY.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());
答案 2 :(得分:0)
在Eclipse中右键单击项目,然后选择Android Tools添加支持库然后安装
将会发生以下变化:
import android.support.v4.app.NotificationCompat;
NotificationCompat.Builder noti = new NotificationCompat.Builder(this);