首页 > 代码库 > 提醒用户的方式 notification+Dialog

提醒用户的方式 notification+Dialog

转载请注明出处: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 = "请打开礼拜提醒!";
		//点击该通知后要跳转的Activity
		Intent notificationIntent = new Intent(this, LuncherActivity.class);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);

		not.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
		//把Notification传递给NotificationManager
		nm.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 {

	@Override
	protected 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() {
			
			@Override
			public 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=http://www.mamicode.com/"@drawable/icon_pray_over" >

配置文件下的

<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

提醒用户的方式 notification+Dialog