转换为通知Compat.Builder

时间:2015-11-12 10:22:00

标签: java android

我试图发布应用通知,但我仍然不知道如何在Uri.parse上做到这一点(&#34;文件:// ........请帮助转换它。非常感谢你< / p>

        // raise notification
        Notification notification = new Notification(appIcon, appName
                + " update", System.currentTimeMillis());
        notification.flags |= NOTIFICATION_FLAGS;

        CharSequence contentTitle = appName + " update available";
        CharSequence contentText = "Select to install";
        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setDataAndType(
                Uri.parse("file://"
                        + context.getFilesDir().getAbsolutePath() + "/"
                        + update_file), ANDROID_PACKAGE);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText,
                contentIntent);
        nm.notify(NOTIFICATION_ID, notification);
    } else {
        nm.cancel(NOTIFICATION_ID);
    }

1 个答案:

答案 0 :(得分:0)

This following code should help you.

NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
    notificationIntent.setDataAndType(
            Uri.parse("file://"
                    + context.getFilesDir().getAbsolutePath() + "/"
                    + update_file), ANDROID_PACKAGE);

//Optional flags according to your requirement
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);

 PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
           .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
           .setSmallIcon(R.drawable.ic_launcher_trans)
           .setContentTitle(title)
           .setContentText(desc)
           .setContentIntent(contentIntent);

    nm.notify(0, mBuilder.build());