通知栏
public class ActivityMainNotification extends Activity {
private static int NOTIFICATIONS_ID = R.layout.activity_notification; //当前页面的布局
private NotificationManager mNotificationManager; //通知管理
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
Button button;
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //初始化管理器
button = (Button) findViewById(R.id.sun_1);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setWeather("晴空万里", "天气预报", "晴空万里", R.drawable.sun);
}
});
button = (Button) findViewById(R.id.cloudy_1);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setWeather("阴云密布", "天气预报", "阴云密布", R.drawable.cloudy);
}
});
button = (Button) findViewById(R.id.rain_1);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setWeather("大雨连绵", "天气预报", "大雨连绵", R.drawable.rain);
}
});
button = (Button) findViewById(R.id.defaultSound);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setDefault(Notification.DEFAULT_SOUND);
}
});
button = (Button) findViewById(R.id.defaultVibrate);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setDefault(Notification.DEFAULT_VIBRATE);
}
});
button = (Button) findViewById(R.id.defaultAll);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setDefault(Notification.DEFAULT_ALL);
}
});
button = (Button) findViewById(R.id.clear);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mNotificationManager.cancel(NOTIFICATIONS_ID); // 关闭一个通知
}
});
}
private void setWeather(String tickerText, String title, String content,
int drawable) {
//建立一个通知实例,第一个参数是图片,第二个标题栏上显示的文字,第三个是时间
Notification notification = new Notification(drawable, tickerText,
System.currentTimeMillis());
//当单击下拉下来的标题内容时候做什么,这里是跳转到主界面。这里和下面是一起的。
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ActivityMain.class), 0);
//Title 是拉下来的标题,Content也是下拉后的内容显示
notification.setLatestEventInfo(this, title, content, contentIntent);
//显示这个通知
mNotificationManager.notify(NOTIFICATIONS_ID, notification);
}
private void setDefault(int defaults) {
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ActivityMain.class), 0);
String title = "天气预报";
String content = "晴空万里";
final Notification notification = new Notification(R.drawable.sun,
content, System.currentTimeMillis());
notification.setLatestEventInfo(this, title, content, contentIntent);
notification.defaults = defaults; //这个是关键的用来说明是否有声音,振动等。
mNotificationManager.notify(NOTIFICATIONS_ID, notification);
}
}
提示框
public class ActivityToast extends Activity {
OnClickListener listener1 = null;
OnClickListener listener2 = null;
Button button1;
Button button2;
private static int NOTIFICATIONS_ID = R.layout.activity_toast;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener1 = new OnClickListener() {
public void onClick(View v) {
setTitle("短时间显示Toast");
showToast(Toast.LENGTH_SHORT);
}
};
listener2 = new OnClickListener() {
public void onClick(View v) {
setTitle("长时间显示Toast");
showToast(Toast.LENGTH_LONG);
showNotification();
}
};
setContentView(R.layout.activity_toast);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(listener1);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(listener2);
}
protected void showToast(int type) {
View view = inflateView(R.layout.toast);//显示布局,从XML构造出View
TextView tv = (TextView) view.findViewById(R.id.content);//设置显示内容
tv.setText("加入专业Android开发社区eoeAndroid.com,让你的应用开发能力迅速提高");
Toast toast = new Toast(this);
toast.setView(view);
toast.setDuration(type); //显示长还是短
toast.show(); //显示
}
private View inflateView(int resource) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return vi.inflate(resource, null);
}
// 这段就是可以同时显示标题栏的提示
protected void showNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//CharSequece 和String 类型一个意思
CharSequence title = "最专业的Android应用开发社区";
CharSequence contents = "eoeandroid.com";
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, ActivityMain.class), 0);
Notification notification = new Notification(R.drawable.default_icon,
title, System.currentTimeMillis());
notification.setLatestEventInfo(this, title, contents, contentIntent);
// 100ms延迟后,振动250ms,停止100ms后振动500ms
notification.vibrate = new long[] { 100, 250, 100, 500 };
notificationManager.notify(NOTIFICATIONS_ID, notification);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2009-05-13 Visual Studio 2008(Device Emulator 3.0)下模拟Windows Mobile 6.0(转自CSDN Imoagn博客)
2009-05-13 2009 年 5 月 忙碌的一个月