提醒用户的方式 notification+Dialog

来源:互联网 发布:中国网络视频用户规模 编辑:程序博客网 时间:2024/06/06 11:41

转载请注明出处:http://blog.csdn.net/harryweasley/article/details/42168079谢谢


我们知道,用户可能下载了Android应用后,以后就可能不会再次启动了,那么为了提醒用户再次启动这个应用完成相应的工作,我做了这个功能。


当然做这个功能的前提是,你得有个服务在后台运行,并且保证服务不会被系统杀死。


我用了两种方式来做这个功能,第一种是利用notificationManager,第二种是利用AlertDialog方法,自动打开屏幕,展现在桌面背景之上。


先看下效果图:



后面的背景是桌面背景,是在配置文件进行设置的。




利用notificationManager方法:

private void notification() {NotificationManager nm;Notification not;nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);not = new Notification(R.drawable.ic_launcher, "打开礼拜提醒",System.currentTimeMillis());//伴有系统默认声音not.defaults |= Notification.DEFAULT_SOUND;//伴有默认闪关灯not.defaults |= Notification.DEFAULT_LIGHTS;//在通知栏上点击此通知后自动清除此通知not.flags |= Notification.FLAG_AUTO_CANCEL;//通知栏标题CharSequence contentTitle = "礼拜提醒";//通知栏内容CharSequence contentText = "请打开礼拜提醒!";//点击该通知后要跳转的ActivityIntent notificationIntent = new Intent(this, LuncherActivity.class);PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);not.setLatestEventInfo(this, contentTitle, contentText, contentIntent);//把Notification传递给NotificationManagernm.notify(0, not);}



利用弹框方式:

跳转的intent

Intent alert = new Intent(this, AlertDialogActivity.class);alert.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);this.startActivity(alert);


alert.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);


AlertDialogActivity 下的代码

package com.bcinfo.pray.ui.activity;import com.bcinfo.pray.R;import com.bcinfo.pray.clock.set.Alarms;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.view.View.OnClickListener;import android.widget.TextView;public class AlertDialogActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//这里是让手机屏幕点亮final Window win = getWindow();        win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED                | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);        // Turn on the screen unless we are being launched from the AlarmAlert        // subclass.        if (!getIntent().getBooleanExtra("screen_off", false))        {            win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON                    | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);        }                setContentView(R.layout.alarm_alert2);findViewById(R.id.dismiss).setOnClickListener(new OnClickListener()        {            public void onClick(View v)            {                finish();            }        });TextView snooze = (TextView) findViewById(R.id.snooze);        snooze.requestFocus();        snooze.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(AlertDialogActivity.this,LuncherActivity.class));finish();}});}}

alarm_alert2下的代码是

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center" >    <LinearLayout        android:id="@+id/id_notify_dialog"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_marginLeft="20dip"        android:layout_marginRight="20dip"        android:background="#ffffffff"        android:gravity="center_horizontal"        android:orientation="vertical" >        <RelativeLayout            android:id="@+id/id_share_part"            android:layout_width="fill_parent"            android:background="@color/white"            android:layout_height="wrap_content" >            <TextView                android:id="@+id/alertTitle"                style="?android:attr/textAppearanceMedium"                android:layout_width="fill_parent"                android:layout_height="40dip"                android:background="@drawable/top_corner_bg"                android:ellipsize="end"                android:gravity="center"                android:padding="5dip"                android:singleLine="true"                android:text="提醒" />                     <TextView                android:id="@+id/id_pray_des"                style="?android:attr/textAppearanceMedium"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:layout_below="@id/alertTitle"                android:layout_marginLeft="20dip"                android:layout_marginRight="20dip"                android:gravity="left"                android:paddingTop="30dp"                android:paddingBottom="30dip"                android:text="请重新打开祈祷提醒,以更新位置,时间等信息"                android:textColor="@color/text_black" />            <ImageView                android:id="@+id/id_pray_over_flag"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginRight="10dip"                android:layout_marginTop="10dip"                android:layout_alignParentTop="true"                android:layout_alignParentRight="true"                android:src="@drawable/icon_pray_over"                 android:visibility="gone"/>        </RelativeLayout>        <RelativeLayout            android:layout_width="fill_parent"            android:layout_height="42dip"            android:layout_marginTop="0dip" >            <TextView                android:layout_width="fill_parent"                android:layout_height="1dip"                android:layout_alignParentTop="true"                android:background="@color/set_edge" />            <TextView                android:layout_width="1dip"                android:layout_height="fill_parent"                android:layout_centerInParent="true"                android:background="@color/set_edge" />            <LinearLayout                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:orientation="horizontal" >                <TextView                    android:id="@+id/snooze"                    style="?android:attr/textAppearanceMedium"                    android:layout_width="fill_parent"                    android:layout_height="fill_parent"                    android:layout_weight="1"                    android:gravity="center"                    android:text="确定"                    android:textColor="@color/title_color" />                <TextView                    android:id="@+id/dismiss"                    style="?android:attr/textAppearanceMedium"                    android:layout_width="fill_parent"                    android:layout_height="fill_parent"                    android:layout_weight="1"                    android:gravity="center"                    android:text="关闭"                    android:textColor="@color/title_color" />            </LinearLayout>        </RelativeLayout>    </LinearLayout></LinearLayout>


配置文件下的

<activity            android:name=".ui.activity.AlertDialogActivity"            android:theme="@android:style/Theme.Wallpaper.NoTitleBar" />

android:theme="@android:style/Theme.Wallpaper.NoTitleBar"这句话是让AlertDialogActivity的背景是手机墙纸背景。


最后要记得加权限

<uses-permission android:name="android.permission.WRITE_SETTINGS" /><uses-permission android:name="android.permission.WAKE_LOCK" />


关于notificationManager更多知识点,可以点击这里查看http://www.cnblogs.com/jerrychoi/archive/2010/05/28/1746221.html

1 0