概述
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的各种属性的设置。
最后
以上就是欢呼玉米为你收集整理的典型的notification一例——“点击Notification,打开浏览器”的全部内容,希望文章能够帮你解决典型的notification一例——“点击Notification,打开浏览器”所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复