<?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" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#EEE" android:textStyle="bold" android:textSize="25sp" android:text="NotificationDemo实例" /> <Button android:id="@+id/btnSend" android:text="send notification" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"/> </LinearLayout>
package com.andyidea.notification;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btnSend;
//定义BroadcastReceiver的action
private static final String NotificationDemo_Action = "com.andyidea.notification.NotificationDemo_Action";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSend = (Button)findViewById(R.id.btnSend);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(NotificationDemo_Action);
sendBroadcast(intent);
}
});
}
}
<?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"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:textColor="#EEE" android:textStyle="bold" android:textSize="25sp" android:text="显示通知界面" /> <Button android:id="@+id/btnCancel" android:text="cancel notification" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> </LinearLayout>
package com.andyidea.notification;
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;
public class SecondActivity extends Activity {
private Button btnCancel;
//声明Notification
private Notification notification;
//声明NotificationManager
private NotificationManager mNotification;
//标识Notification的ID
private static final int ID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondlayout);
btnCancel = (Button)findViewById(R.id.btnCancel);
//怎样获得NotificationManager的实例?
String service = NOTIFICATION_SERVICE;
mNotification = (NotificationManager)getSystemService(service);
//获得Notification的实例
notification = new Notification();
//设置该图标 会在状态栏显示
int icon = notification.icon = android.R.drawable.stat_sys_phone_call;
//设置提示信息
String tickerText = "Test Notification";
//设置显示时间
long when = System.currentTimeMillis();
notification.icon = icon;
notification.tickerText = tickerText;
notification.when = when;
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "消息", "SMS Android", pi);
mNotification.notify(ID, notification);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mNotification.cancel(ID); //--->取消通知
}
});
}
}
package com.andyidea.notification;
import com.andyidea.notification.SecondActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//实例化Intent
Intent i = new Intent();
//在新任务中启动Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//设置Intent启动的组件名称
i.setClass(context, SecondActivity.class);
//启动Activity,显示通知
context.startActivity(i);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有