Android开发笔记——通知栏设置
2012-12-11 09:23
567 查看
手机有未接来电或未读短信时在通知栏就可以有一个状态,下拉后点击相应条目就可以进入此条目,同时通知栏上的此条信息消失,其实实现起来并不难,通过一个notification和notificationmanager即可
我下面一个activity和一个intentservice实现,模拟下载程序:
源码如下:
package com.example.activity;
import com.example.service.IntentServiceDemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
@Override
protected void onStart()
{
//点击bar跳转后bar消失,由于oncreate只有在创建时才条用故放在此方法中
super.onStart();
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.cancel(R.layout.main);
}
Button btn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{//点击按钮跳转至service,记得要在manifest文件中声明一下
Intent intent=new Intent(Main.this,IntentServiceDemo.class);
startService(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
点击按钮触发intentservice服务,
注意到通知栏出现开始下载条目,5s过后下载完毕
返回主菜单拉下通知栏
点击条目可以来到main,同时通知栏中的条目消失
========================================
package com.example.service;
import com.example.activity.*;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;
public class IntentServiceDemo extends IntentService
{
//需要一个空的构造方法,继承父类的名字
public IntentServiceDemo()
{
super("IntentServiceDemo");
}
@Override
//重写onhandleintent方法
protected void onHandleIntent(Intent intent)
{
Log.i("TAG","开始下载");
//调用此函数 激发下载进程
showNotification(true);
try
{
//进程休眠5s,模拟下载过程
Thread.sleep(5000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//下载完毕,再次调用
showNotification(false);
Log.i("TAG","下载完毕");
}
private void showNotification(boolean b)
{
Notification notification;//声明一个notification
Intent intent=new Intent(this,Main.class);
//pendingintent中封装了intent,跳转到Main
PendingIntent contentIntent=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);//获取一个notificationmanager
if (b)//如果b为真表示还未下载成功开始下载
{//三个参数分别为图标、标题、开始时间
notification=new Notification(R.drawable.ok, "开始下载", System.currentTimeMillis());
//下拉通知栏后显示的信息,点击后执行contentintent
notification.setLatestEventInfo(this, "下载", "正在下载…", contentIntent);//用户点击时运行contentintent
}else//否则为假 表示下载完毕
{
notification=new Notification(R.drawable.ok, "下载完毕", System.currentTimeMillis());
notification.setLatestEventInfo(this, "下载", "下载完毕!", contentIntent);
//可以指定通知的方式,如铃声、震动、LED彩灯等
//notification.defaults=Notification.DEFAULT_LIGHTS;
//notification.defaults=Notification.DEFAULT_SOUND;
}
//下载完毕后通知,参数为一个id(唯一),这里可以指定为R.layout.main,但前后要保持一致;另一个用来指定notification
nm.notify(R.layout.main, notification);//id唯一的
}
}
我下面一个activity和一个intentservice实现,模拟下载程序:
源码如下:
package com.example.activity;
import com.example.service.IntentServiceDemo;
import android.os.Bundle;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
@Override
protected void onStart()
{
//点击bar跳转后bar消失,由于oncreate只有在创建时才条用故放在此方法中
super.onStart();
NotificationManager manager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.cancel(R.layout.main);
}
Button btn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{//点击按钮跳转至service,记得要在manifest文件中声明一下
Intent intent=new Intent(Main.this,IntentServiceDemo.class);
startService(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
点击按钮触发intentservice服务,
注意到通知栏出现开始下载条目,5s过后下载完毕
返回主菜单拉下通知栏
点击条目可以来到main,同时通知栏中的条目消失
========================================
package com.example.service;
import com.example.activity.*;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;
public class IntentServiceDemo extends IntentService
{
//需要一个空的构造方法,继承父类的名字
public IntentServiceDemo()
{
super("IntentServiceDemo");
}
@Override
//重写onhandleintent方法
protected void onHandleIntent(Intent intent)
{
Log.i("TAG","开始下载");
//调用此函数 激发下载进程
showNotification(true);
try
{
//进程休眠5s,模拟下载过程
Thread.sleep(5000);
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//下载完毕,再次调用
showNotification(false);
Log.i("TAG","下载完毕");
}
private void showNotification(boolean b)
{
Notification notification;//声明一个notification
Intent intent=new Intent(this,Main.class);
//pendingintent中封装了intent,跳转到Main
PendingIntent contentIntent=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);//获取一个notificationmanager
if (b)//如果b为真表示还未下载成功开始下载
{//三个参数分别为图标、标题、开始时间
notification=new Notification(R.drawable.ok, "开始下载", System.currentTimeMillis());
//下拉通知栏后显示的信息,点击后执行contentintent
notification.setLatestEventInfo(this, "下载", "正在下载…", contentIntent);//用户点击时运行contentintent
}else//否则为假 表示下载完毕
{
notification=new Notification(R.drawable.ok, "下载完毕", System.currentTimeMillis());
notification.setLatestEventInfo(this, "下载", "下载完毕!", contentIntent);
//可以指定通知的方式,如铃声、震动、LED彩灯等
//notification.defaults=Notification.DEFAULT_LIGHTS;
//notification.defaults=Notification.DEFAULT_SOUND;
}
//下载完毕后通知,参数为一个id(唯一),这里可以指定为R.layout.main,但前后要保持一致;另一个用来指定notification
nm.notify(R.layout.main, notification);//id唯一的
}
}
相关文章推荐
- Android开发笔记: Android Studio设置app全屏
- Android(java)学习笔记163:Android开发时候颜色设置是bgr不是rgb
- android应用开发揭秘examples_04-11笔记(DatePicker,TimePicker时间日期设置,略有改动)
- Android开发笔记之主题背景的修改(设置Theme.NoTitleBar后Activity背景变成黑色)
- Android开发笔记之Android Studio 1.3.0 以上gradle proxy代理设置异常
- android开发学习笔记(7)设置android模拟器为中文环境
- Android开发学习笔记之Activity属性设置
- ios海哥开发笔记 (海哥原创,UILocalNotification本地通知的设置以及iOS9通知新特性 )
- Android开发笔记(五十二)通知推送Notification
- Android开发笔记-Eclipse和Android Studio的偏好设置Preferences的导出与导入
- Android开发学习笔记(6):在虚拟机里创建与设置SDCard
- android开发通知常用设置方法
- Android开发笔记之notification消息推送 通知栏的实现
- Android应用开发学习笔记之状态栏通知
- Android开发学习笔记:Android Studio-设置switch/case代码块自动补齐
- android通知栏通知的开发笔记
- Android开发实现判断通知栏是否打开及前往设置页面的方法
- Android开发总结笔记 Notification(消息通知) 1-1-20
- Android 通知栏自定义视图并且设置事件的开发
- Android开发学习笔记之设置渐变颜色