[quote]
最近一直在研究android,并一边研究一边做应用。其中遇到了把程序通知常驻在Notification栏,并且不能被clear掉的问题。虽然notify()的第一个参数可以写死并clear掉,但这个值我并不想写死,但是这个值如果是随机生成一个数怎么传给Activity,用Intent当中的Bundle是可以传值过去,但是一个Notification消息是没问题,一但有多个消息,这个值就不管用了,大家有什么好的方法?
[/quote]
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/button"
android:text="显示消息提示" />
</LinearLayout>
notification_layout.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<EditText android:text="启动Notification" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:editable="false"
android:cursorVisible="false" android:layout_gravity="center_horizontal" />
</LinearLayout>
IaiaiActivity.java类:
package com.iaiai.activity;
import java.util.Date;
import java.util.Random;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
*
* <p>
* Title: IaiaiActivity.java
* </p>
* <p>
* E-Mail: [email protected]
* </p>
* <p>
* QQ: 176291935
* </p>
* <p>
* Http: iaiai.iteye.com
* </p>
* <p>
* Create time: 2011-6-26
* </p>
*
* @author 丸子
* @version 0.0.1
*/
public class IaiaiActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
PendingIntent pending = PendingIntent.getActivity(
IaiaiActivity.this, 0, new Intent(IaiaiActivity.this,
NotificationActivity.class), 0);
Notification noticed = new Notification();
noticed.icon = R.drawable.icon;
noticed.tickerText = "状态栏通知";
// noticed.defaults = Notification.DEFAULT_SOUND; // 使用默认的声音
// noticed.defaults = Notification.DEFAULT_VIBRATE; // 使用默认的震动
// noticed.defaults = Notification.DEFAULT_LIGHTS; // 使用默认的Light
// noticed.defaults = Notification.DEFAULT_ALL; // 所有的都使用默认值
noticed.defaults = Notification.DEFAULT_SOUND;
//设置音乐
// noticed.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
// noticed.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
noticed.setLatestEventInfo(IaiaiActivity.this, "这是一个状态栏通知",
"查看", pending);
NotificationManager noticedManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noticedManager.notify(0, noticed);
}
};
button.setOnClickListener(listener);
}
public int getRand() {
Date date = new Date();
int year = date.getYear();
int month = date.getMonth();
int day = date.getDate();
return 0;
}
}
NotificationActivity.java类:
package com.iaiai.activity;
import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
/**
*
* <p>
* Title: NotificationActivity.java
* </p>
* <p>
* E-Mail: [email protected]
* </p>
* <p>
* QQ: 176291935
* </p>
* <p>
* Http: iaiai.iteye.com
* </p>
* <p>
* Create time: 2011-6-26
* </p>
*
* @author 丸子
* @version 0.0.1
*/
public class NotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_layout);
Bundle bundle = getIntent().getExtras();
NotificationManager noticedManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noticedManager.cancel(0);
}
}
运行结果:
[img]http://dl.iteye.com/upload/attachment/505287/e52382b1-3f91-3474-886d-3d1a5710804f.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/505288/584e667c-d404-37e2-8dad-86f9f23d78e4.jpg[/img]