创建和触发Notification

编程--Android类 No Comments »

创建Notification

创建和配置新的Notification需要经历三步。

首先,你要创建一个新的Notification对象,传入要在状态条上显示的图标、文字和Notification的当前时间,如下面的代码片段所示:

// Choose a drawable to display as the status bar icon

int icon = R.drawable.icon;

// Text to display in the status bar when the notification is launched

String tickerText = “Notification”;

// The extended status bar orders notification in time order

long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);

当Notification触发时,文本将沿着状态条进行滚动显示。

其次,使用setLatestEventInfo方法来配置Notification在扩展的状态窗口中的外观。扩展的状态窗口将显示图标和在构造函数中传入的时间,以及显示标题和一个详细的字符串。Notification一般表示为对一个动作的请求或引起用户的注意,所以,当用户点击Notification项目时你可以指定一个PendingIntent来触发。

下面的代码片段使用了setLastestEventInfo来设置这些值:

Context context = getApplicationContext();

// Text to display in the extended status window

String expandedText = “Extended status text”;

// Title for the expanded status

String expandedTitle = “Notification Title”;

// Intent to launch an activity when the extended text is clicked

Intent intent = new Intent(this, MyActivity.class);

PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);

notification.setLatestEventInfo(context, expandedTitle, expandedText, launchIntent);

一个好的形式是显示相同事件(例如,接收多个SMS消息)的多个对象时使用一个Notification图标。为了呈现给用户,使用setLastestEventInfo更新数据集来呈现最近的消息以及重新触发Notification来更新它的值。

你还可以使用number属性来显示一个状态条图标所表示的事件数目。

设置为比1大的数,如下所示,将在状态条图标上以一个小小的数字进行显示:

notification.number++;

任何对Notification的变更,你都需要重新触发来进行更新。如果要删除这个数字,设置number的值为0或者-1。

最后,你可以对Notification对象使用多种属性来增强Notification的效果,如闪烁LED、震动电话和播放音乐文件。这些高级特征将在本章的后面部分进行详细描述。

触发Notification

为了触发一个Notification,使用NotificationManager的notify方法,将一个整数的ID和Notification对象传入,如下的片段所示:

int notificationRef = 1;

notificationManager.notify(notificationRef, notification);

为了更新一个已经触发过的Notification,传入相同的ID。你既可以传入相同的Notification对象,也可以是一个全新的对象。只要ID相同,新的Notification对象会替换状态条 图标和扩展的状态窗口的细节。

你还可以使用ID来取消Notification,通过调用NotificationManager的cancel方法,如下所示:

notificationManager.cancel(notificationRef);

取消一个Notification时,将移除它的状态条图标以及清除在扩展的状态窗口中的信息。

Android — NotificationManager and Notification学习笔记

编程--Android类 No Comments »

NotificationManager(通知管理器):
NotificationManager负责通知用户事件的发生.
NotificationManager有三个公共方法:
1. cancel(int id) 取消以前显示的一个通知.假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走.
2. cancelAll() 取消以前显示的所有通知.
3. notify(int id,  Notification notification) 把通知持久的发送到状态条上.

  • //初始化NotificationManager:   
  • NotificationManager nm =   
  •       (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  

 

Notification代表着一个通知.
Notification的属性:
audioStreamType 当声音响起时,所用的音频流的类型
contentIntent 当通知条目被点击,就执行这个被设置的Intent.
contentView 当通知被显示在状态条上的时候,同时这个被设置的视图被显示.
defaults 指定哪个值要被设置成默认的.
deleteIntent 当用户点击”Clear All Notifications”按钮区删除所有的通知的时候,这个被设置的Intent被执行.
icon 状态条所用的图片.
iconLevel 假如状态条的图片有几个级别,就设置这里.
ledARGB LED灯的颜色.
ledOffMS LED关闭时的闪光时间(以毫秒计算)
ledOnMS LED开始时的闪光时间(以毫秒计算)
number 这个通知代表事件的号码
sound 通知的声音
tickerText 通知被显示在状态条时,所显示的信息
vibrate 振动模式.
when 通知的时间戳.

Notification的公共方法:
describeContents() Describe the kinds of special objects contained in this Parcelable’s marshalled representation.
setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)     设置Notification留言条的参数
writeToParcel(Parcel parcel, int flags) Flatten this notification from a parcel.
toString()    …………….

将Notification发送到状态条上:

  • Notification notification = new Notification();   
  • Notification的设置过程……..   
  • nm.notify(0, notification);   //发送到状态条上  

如何设置隐藏标题栏和状态栏?

编程--Android类 No Comments »

G1的屏幕的分辨率是320×480,虽说不小但也谈不上大,所以有的应用要使用起来,最好还是希望把状态栏(Status Bar)和标题栏(Title Bar)也隐藏了,这样就可以有更大的屏幕空间给应用程序。

  1. @Override
  2. public void onCreate(Bundle icicle) {
  3. super.onCreate(icicle);
  4. final Window win = getWindow();
  5. // No Statusbar
  6. win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  7. WindowManager.LayoutParams.FLAG_FULLSCREEN);
  8. // No Titlebar
  9. requestWindowFeature(Window.FEATURE_NO_TITLE);
  10. setContentView(R.layout.mylayout);
  11. }

用Intent调用系统中经常被用到的组件

编程--Android类 No Comments »

1,web浏览器

Uri uri= Uri.parse(“http://kuikui.javaeye.com”);

returnIt = new Intent(Intent.ACTION_VIEW, uri);

2,地图

Uri mapUri = Uri.parse(“geo:38.899533,-77.036476″);

returnIt = new Intent(Intent.ACTION_VIEW, mapUri);

3,调拨打电话界面

Uri telUri = Uri.parse(“tel:100861″);

returnIt = new Intent(Intent.ACTION_DIAL, telUri);

4,直接拨打电话

Uri callUri = Uri.parse(“tel:100861″);

returnIt = new Intent(Intent.ACTION_CALL, callUri);

5,卸载

Uri uninstallUri = Uri.fromParts(“package”, “xxx”, null);

returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);

6,安装

Uri installUri = Uri.fromParts(“package”, “xxx”, null);

returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);

7,播放

Uri playUri = Uri.parse(“file:///sdcard/download/everything.mp3″);

returnIt = new Intent(Intent.ACTION_VIEW, playUri);

8,调用发邮件

Uri emailUri = Uri.parse(“mailto:shenrenkui@gmail.com”);

returnIt = new Intent(Intent.ACTION_SENDTO, emailUri);

9,发邮件

returnIt = new Intent(Intent.ACTION_SEND);

String[] tos = { “shenrenkui@gmail.com” };

String[] ccs = { “shenrenkui@gmail.com” };

returnIt.putExtra(Intent.EXTRA_EMAIL, tos);

returnIt.putExtra(Intent.EXTRA_CC, ccs);

returnIt.putExtra(Intent.EXTRA_TEXT, “body”);

returnIt.putExtra(Intent.EXTRA_SUBJECT, “subject”);

returnIt.setType(“message/rfc882″);

Intent.createChooser(returnIt, “Choose Email Client”);

10,发短信

Uri smsUri = Uri.parse(“tel:100861″);

returnIt = new Intent(Intent.ACTION_VIEW, smsUri);

returnIt.putExtra(“sms_body”, “shenrenkui”);

returnIt.setType(“vnd.android-dir/mms-sms”);

11,直接发邮件

Uri smsToUri = Uri.parse(“smsto://100861″);

returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri);

returnIt.putExtra(“sms_body”, “shenrenkui”);

12,发彩信

Uri mmsUri = Uri.parse(“content://media/external/images/media/23″);

returnIt = new Intent(Intent.ACTION_SEND);

returnIt.putExtra(“sms_body”, “shenrenkui”);

returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri);

returnIt.setType(“image/png”);

Android中ListView动态添加删除项

编程--Android类 No Comments »

首先是创建三个全局变量:

SimpleAdapter listItemAdapter;  // ListView的适配器
ArrayList<HashMap<String, Object>> listItem;  // ListView的数据源,这里是一个HashMap的列表
ListView myList;  // ListView控件

然后在Activity的onCreate函数中对变量进行初始化:

listItem = new ArrayList<HashMap<String, Object>>();
listItemAdapter = new SimpleAdapter(this, listItem, R.layout.mylayout,
new String[]{“image”, “title”, “text”},
new int[]{R.id.ItemImage, R.id.ItemTitle, R.id.ItemText});
myList = (ListView)findViewById(R.id.TaxiList);
myList.setAdapter(listItemAdapter);

添加两个私有的功能函数:
private void addItem()
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(“image”, R.drawable.icon);
map.put(“title”, “标题”);
map.put(“text”, “要显示的内容”);
listItem.add(map);
listItemAdapter.notifyDataSetChanged();
}

private void deleteItem()
{
int size = listItem.size();
if( size > 0 )
{
listItem.remove(listItem.size() – 1);
listItemAdapter.notifyDataSetChanged();
}
}

另附上ListView的项自定义的Layout不再多说:
<?xml version=”1.0″ encoding=”utf-8″?> 
<RelativeLayout  
android:id=”@+id/RelativeLayout01″  
android:layout_width=”fill_parent”  
xmlns:android=”http://schemas.android.com/apk/res/android”  
android:layout_height=”wrap_content”  
android:paddingBottom=”4dip”  
android:paddingLeft=”12dip” 
android:paddingRight=”12dip”> 
<ImageView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/taxi1″
android:id=”@+id/ItemImage”
android:paddingTop=”4dip”>
</ImageView>
<TextView
android:layout_height=”wrap_content”
android:layout_width=”fill_parent”
android:text=”DaZhong Taxi Corporation”
android:layout_toRightOf=”@+id/ItemImage”
android:id=”@+id/ItemTitle”
android:textSize=”24dip”></TextView>
<TextView
android:layout_height=”wrap_content”
android:layout_width=”fill_parent”
android:text=”Tel:021-67786874″
android:id=”@+id/ItemText”
android:layout_below=”@+id/ItemTitle”
android:layout_toRightOf=”@+id/ItemImage”>
</TextView>
</RelativeLayout>