典型的notification一例——“点击Notification,打开浏览器”

1. 本例功能: 点击Notification,打开浏览器

2. 代码:


public static void showNotification(Context context, strUrl) {
 
    final int NOTIFY_ID = 999;
    NotificationManager manager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);

Notification myNoti = new Notification();

myNoti.flags |= Notification.FLAG_AUTO_CANCEL;

Intent intent = getBrowserAppIntent(context,strUrl);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT);

myNoti.tickerText = "Notify";
myNoti.icon = R.drawable.ic_launcher;
//myNoti.defaults = Notification.DEFAULT_SOUND;
myNoti.setLatestEventInfo(context,
context.getString(R.string.app_name), strUrl,
pendingIntent);

manager.notify(NOTIFY_ID, myNoti);
//manager.cancel(NOTIFY_ID);

return;

    }
    
    private static Intent getBrowserAppIntent(Context context,String strUrl) {
    ActivityInfo browserActivity = getBrowserMainActivity(context);
    if (browserActivity != null) {
    Intent intent = new Intent();        
    intent.setAction("android.intent.action.VIEW");        
    Uri content_uri_browsers = Uri.parse(strUrl);       
    intent.setData(content_uri_browsers);        
    intent.setClassName(browserActivity.packageName, browserActivity.name);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  
    return intent;
    }
    return null;
    }


private static ActivityInfo getBrowserApp(Context context) {
String default_browser = "android.intent.category.DEFAULT";
String browsable = "android.intent.category.BROWSABLE";
String view = "android.intent.action.VIEW";


Intent intent = new Intent(view);
intent.addCategory(default_browser);
intent.addCategory(browsable);
Uri uri = Uri.parse("http://");
intent.setDataAndType(uri, null);


// 找出手机当前安装的所有浏览器程序
List<ResolveInfo> resolveInfoList = context.getPackageManager()
.queryIntentActivities(intent,
PackageManager.GET_INTENT_FILTERS);


if (resolveInfoList.size() > 0) {
ActivityInfo activityInfo = resolveInfoList.get(0).activityInfo;
String packageName = activityInfo.packageName;
String className = activityInfo.name;


Log.i(TAG, "packageName  " + packageName);
Log.i(TAG, "className  " + className);
return activityInfo;
} else {
return null;
}
}

3.  补充与说明:

     1) intent也可以用setCompnent来设置。

     2)注意获取browser应用的方式

     3)   intent的各种属性的设置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值