显示定制对话框在intentservice [英] Show custom dialog in an intentservice

查看:233
本文介绍了显示定制对话框在intentservice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个应用程序将接收GCM通知,并告诉他们作为一个对话框。有没有什么办法来实现code,以显示意图服务的对话?

i am creating an app which will receive GCM notifications and show them as a dialog. is there any way to implement code to show a dialog in Intent Service?

推荐答案

有根据我的方法有两种。

There are two ways according to me.


  1. 在点击通知中,打开一个对话框或对话活动
    需求。

  2. 使用BigSt​​yle通知,但它需要超过16 API级别

打开对话框

/**
 * Issues a notification to inform the user that server has sent a message.
 */
private void generateNotification(Intent intent) {

    int requestID = (int) System.currentTimeMillis(); // Some changes required to work on 4.4 kitkat 

    // Notification Builder 
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Title")
            .setContentText("Content of notification")
            .setLargeIcon(
                    BitmapFactory.decodeResource(getResources(),
                            R.drawable.ic_launcher));

    // set default Ringtone 
    mBuilder.setSound(RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    mBuilder.setAutoCancel(true);

    // open yopur dialog activity on Click of notification 
    notificationIntent = new Intent(this, DialogActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    // Getting data From GCM
    if (intent.getExtras().getString("data") != null
        // passing data to Dialog
        notificationIntent.putExtra("data", your_string_data);
    }
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    // Pending intent shouid be PendingIntent.FLAG_UPDATE_CURRENT for data
    PendingIntent pIntent = PendingIntent.getActivity(this, requestID,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(pIntent);

    NotificationManager notificationManager = (NotificationManager)    getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(sNotificationId++, mBuilder.build());
}

大牌风范通知

// know device version
public static final int build =  Build.VERSION.SDK_INT;
if(build >= 16){

    // Big style notifications
    mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    Notification noti = new Notification();     
    // Call method and send required intent   
    noti = setBigTextStyleNotification(intent);

    noti.defaults |= Notification.DEFAULT_LIGHTS;
    noti.defaults |= Notification.DEFAULT_VIBRATE;
    noti.defaults |= Notification.DEFAULT_SOUND;

    noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE;

    mNotificationManager.notify(1, noti);
}


/**
 * Big Text Style Notification
 * 
 * @return Notification
 * @see CreateNotification
 */
private Notification setBigTextStyleNotification(Intent intent) {

    Bitmap remote_picture = null;
    // Get data from intent 
    String trip_date = intent.getExtras().getString("Data");

    // Create the style object with BigTextStyle subclass.
    NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
    notiStyle.setBigContentTitle("Title");
    notiStyle.setSummaryText("");// this contain text like Gmail notification 

    try {
        remote_picture = BitmapFactory.decodeResource(getResources(),
                R.drawable.ic_launcher);// getting icon 

    } catch (Exception e) {
        e.printStackTrace();
    }

    // Add the big text to the style.
    CharSequence bigText = "you have confirmed a new trip."
            + "Add to Calendar";
    notiStyle.bigText(bigText);

    // Creates an explicit intent for an ResultActivity to receive.
    Intent resultIntent = new Intent(this, DashboardActivity.class);

    // This ensures that the back button follows the recommended convention
    // for the back key.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    resultIntent.putExtra("data");
    // Adds the back stack for the Intent (but not the Intent itself).
    stackBuilder.addParentStack(SplashActivity.class);

    // Adds the Intent that starts the Activity to the top of the stack.
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);

    return new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher).setAutoCancel(true)
            .setLargeIcon(remote_picture)
            .setContentIntent(resultPendingIntent)
            .addAction(R.drawable.yes, "", resultPendingIntent) // setting yes or No images 
            .addAction(R.drawable.cancel, "", resultPendingIntent) //Cancel images 
            .setContentTitle("").setContentText("") //Title and Content 
            .setStyle(notiStyle).build();//Style of notifications 
}

使用谷歌文档的通知:

Check Google Docs on notifications:

通知

通知用户

这篇关于显示定制对话框在intentservice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆