Android的Notification,Builder怎样发通知?
直接上代码:publicclassMainActivityextendsActivityimplementsOnClickListener{privateButtontv...
直接上代码:
public class MainActivity extends Activity implements OnClickListener{
private Button tvTest;
private NotificationManager notification;
private Builder b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTest=(Button)findViewById(R.id.tvTest);
tvTest.setOnClickListener(this);
notification=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
b=new Builder(this).setTicker("重要通知").setAutoCancel(true).setContentTitle("邮 件").setContentText("你好啊,小妹嘿嘿");
}
@Override
public void onClick(View v) {
notification.notify(123, b.build());
System.out.println("发送Notification...");
}
}
很简单的代码呀,怎么就发不出Notification呢,代码问题,还是模拟器问题? 展开
public class MainActivity extends Activity implements OnClickListener{
private Button tvTest;
private NotificationManager notification;
private Builder b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTest=(Button)findViewById(R.id.tvTest);
tvTest.setOnClickListener(this);
notification=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
b=new Builder(this).setTicker("重要通知").setAutoCancel(true).setContentTitle("邮 件").setContentText("你好啊,小妹嘿嘿");
}
@Override
public void onClick(View v) {
notification.notify(123, b.build());
System.out.println("发送Notification...");
}
}
很简单的代码呀,怎么就发不出Notification呢,代码问题,还是模拟器问题? 展开
2个回答
展开全部
Notification noti = new Notification.Builder(mContext)
.setContentTitle("New mail from " )
.setContentText(“gmail”)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build();
notification.notify(0, noti);
也可以这样写:
NotificationManager mNm = (NotificationManager)getSystemService(Activity.NOTIFICATION_SERVICE);
CharSequence text = getText(R.string.hello_world);
CharSequence title = getText(R.string.app_name);
Notification notification = new Notification();
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_AUTO_CANCEL;//Notification.FLAG_ONGOING_EVENT;一直显示
//notification.priority = Notification.PRIORITY_MIN;//不显示声音和标题
notification.icon = R.drawable.ic_launcher;
Intent i = new Intent("android.intent.action.MANAGE_PACKAGE_STORAGE");
//i.setClassName("com.android.settings", "com.android.settings.applications.ManageApplications");
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, i, 0);
notification.setLatestEventInfo(this, title, text, pi);
mNm.notify(0, notification);
你自己看着改。。。
.setContentTitle("New mail from " )
.setContentText(“gmail”)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build();
notification.notify(0, noti);
也可以这样写:
NotificationManager mNm = (NotificationManager)getSystemService(Activity.NOTIFICATION_SERVICE);
CharSequence text = getText(R.string.hello_world);
CharSequence title = getText(R.string.app_name);
Notification notification = new Notification();
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags = Notification.FLAG_AUTO_CANCEL;//Notification.FLAG_ONGOING_EVENT;一直显示
//notification.priority = Notification.PRIORITY_MIN;//不显示声音和标题
notification.icon = R.drawable.ic_launcher;
Intent i = new Intent("android.intent.action.MANAGE_PACKAGE_STORAGE");
//i.setClassName("com.android.settings", "com.android.settings.applications.ManageApplications");
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, i, 0);
notification.setLatestEventInfo(this, title, text, pi);
mNm.notify(0, notification);
你自己看着改。。。
追问
我就是没看出来我的代码,哪里有问题呀?为什么显示不出来呢
追答
因为你没有导入呀,要这样吧:Notification.Builder b
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询