Android:在通知中创建意图以启动日历或网络

时间:2014-10-23 12:05:34

标签: android android-intent calendar

我想创建通知,能够在日历中创建通知的事件或启动更多信息的网页。

所以我的代码是:

private void sendNotification(String title, String msg, String website, String startDate, String endDate) {
    long eventStartInMillis = 0, eventEndInMillis = 0;
    Notification.Builder mBuilder = null;

    mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

    Uri ringURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent webIntent = new Intent(Intent.ACTION_VIEW);

    try {
        if (website != null) webIntent.setData(Uri.parse(website));
    } catch (NullPointerException e) {
        Log.d(Constants.APP_TAG, "NullPointerException " + e);
    }

    PendingIntent webPendingIntent = PendingIntent.getActivity(this, 0, webIntent, 0);

    try {
        Date startEventDate = new SimpleDateFormat("dd/MM/yy hh:mm", Locale.getDefault()).parse(startDate);
        Date endEventDate = new SimpleDateFormat("dd/MM/yy hh:mm", Locale.getDefault()).parse(endDate);
        eventStartInMillis = startEventDate.getTime();
        eventEndInMillis = endEventDate.getTime();
    } catch (ParseException e) {
        Log.d(Constants.APP_TAG, "ParseException " + e);
    }

    Intent calIntent = new Intent(Intent.ACTION_INSERT);

    calIntent.setData(Events.CONTENT_URI);
    calIntent.putExtra(Events.TITLE, title);
    calIntent.putExtra(Events.DESCRIPTION, msg);
    calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, eventStartInMillis); 
    calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, eventEndInMillis);

    PendingIntent calPendingIntent = PendingIntent.getActivity(this, 0, calIntent, 0);

    mBuilder = new Notification.Builder(this)
    .setSmallIcon(R.drawable.icon_guide_48)
    .setContentTitle(title)
    .setSound(ringURI)
    .setStyle(new Notification.BigTextStyle().bigText(msg))
    .setContentText(msg)
    .setContentIntent(contentIntent)
    .addAction(R.drawable.calendar_48, "Note it!", calPendingIntent)
    .addAction(R.drawable.web_48, "More infos", webPendingIntent)
    ;

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

因此启动了日历或Web浏览器,但没有任何数据;创建的事件没有标题,没有描述,没有日期......,并且网络没有网址开始。

我的错误是什么?

此致

2 个答案:

答案 0 :(得分:0)

您需要设置意图的属性,如下所示:

Intent i = new Intent("android.intent.action.VIEW");
            i.setData(Uri.parse("http://www.nscc.ca"));
            startActivity(i);

检查website变量状态的有效性。

答案 1 :(得分:0)

所以,我发现ACTION_INSERT不是添加预填充事件的正确方法,我们必须使用ACTION_EDIT。

对于那些对如何生成能够在地图上找到它的通知事件感兴趣的人,通过选择您喜欢的地图应用程序,将其添加到日历中或访问更多信息'网页,一切都在下面:

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void sendNotification(String title, String msg, String location, String latitude, String longitude, String website, String startDate, String endDate) {
    Date startEventDate = null;
    Date endEventDate = null;

    NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(this);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

    Uri ringURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


    Intent webIntent = new Intent("android.intent.action.VIEW");

    try {
        if (website != null) webIntent.setData(Uri.parse(website));
    } catch (NullPointerException e) {
        Log.d(Constants.APP_TAG, "NullPointerException " + e);
    }

    PendingIntent webPendingIntent = PendingIntent.getActivity(this, 0, webIntent, 0);

    //String uri = String.format(Locale.ENGLISH, "geo:%s,%s,ICI", latitude, longitude);
    String uri = String.format(Locale.ENGLISH, "geo:%f,%f", 44.221149, 3.357192);
    Intent geoIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));

    PendingIntent geoPendingIntent = PendingIntent.getActivity(this, 0, geoIntent, 0);


    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {

        try {
            startEventDate = new SimpleDateFormat("dd-MM-yyyy hh:mm", Locale.getDefault()).parse(startDate);
            endEventDate = new SimpleDateFormat("dd-MM-yyyy hh:mm", Locale.getDefault()).parse(endDate);

            Intent calIntent = new Intent(Intent.ACTION_EDIT)
            .setData(Events.CONTENT_URI)
            .putExtra(Events.TITLE, title)
            .putExtra(Events.DESCRIPTION, msg)
            .putExtra(Events.EVENT_LOCATION, location)
            .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startEventDate.getTime()) 
            .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endEventDate.getTime())
            .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
            ;


            PendingIntent calPendingIntent = PendingIntent.getActivity(this, 0, calIntent, 0);
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon_guide_48)
            .setContentTitle(title)
            .setSound(ringURI)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg).setSummaryText(Constants.APP_TAG + "vous informe"))
            .setContentText(msg)
            .setContentIntent(contentIntent)
            .addAction(R.drawable.ic_action_place, null, geoPendingIntent)
            .addAction(R.drawable.ic_action_new_event, null, calPendingIntent)
            .addAction(R.drawable.ic_action_web_site, null, webPendingIntent)
            ;

            mBuilder.setContentIntent(contentIntent);
            mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

        } catch (ParseException e) {
            Log.d(Constants.APP_TAG, "ParseException " + e);
        }

    } else {
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.icon_guide_48)
        .setContentTitle(title)
        .setSound(ringURI)
        .setContentText(msg)
        .setContentIntent(contentIntent)
        .addAction(R.drawable.ic_action_web_site, null, webPendingIntent)
        ;

        mBuilder.setContentIntent(contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

    }

}

好的代码!

此致 阿兰