简体   繁体   English

创建具有待定意图的通知

[英]Creating a notification with a pending intent

SO I am fairly new to android and I am trying to make a button create a notification and then clicking on this notification makes a call. 所以我对android来说还很陌生,我试图创建一个按钮来创建通知,然后单击该通知进行呼叫。 I've done a lot of looking around online and I still can't figure out why this is not working. 我已经在网上进行了很多环顾,但仍然不知道为什么这行不通。 I have this method. 我有这种方法。

   public void notification(){
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel: 0210000001"));
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, callIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    android.support.v4.app.NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_stat_name)
                    .setContentTitle("My notification")
                    .setContentText("Click to ring 021 000 0001")
                    .setContentIntent(pendingIntent);
}

and this button that calls the method. 和调用该方法的按钮。

Button notif = (Button) findViewById(R.id.notificationButton);
    notif.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            notification();
        }
    });}

Any help would be greatly appreciated. 任何帮助将不胜感激。

You need to build the notification from your NotificationBuilder and then notify. 您需要从NotificationBuilder构建NotificationBuilder ,然后进行通知。

Notification mNotification = mBuilder.build();
mNotification.notify();

NotificationManager mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mManager.notify(1234, mNotification);

The number 1234 is a random arbitrary number to identify this notification from your process. 数字1234是一个随机的任意数字,用于从您的进程中标识此通知。 You can put any number and not worry about it unless later you want to manipulate this specific notification (like cancel it). 您可以输入任何数字,而不必担心,除非以后您要操纵此特定通知(例如取消它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM