【问题标题】:How to call a method or phone number using notifcation addAction如何使用通知 addAction 调用方法或电话号码
【发布时间】:2014-11-04 08:19:47
【问题描述】:

我正在尝试调用此方法`或更重要的是,当我按下 addAction 按钮时会调用一个数字。

public void call(){
        Log.i(TAG,"Attempting Call "+emergecyNumber);
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        //callIntent.setData(Uri.parse("tel:" +emergecyNumber));
        startActivity(callIntent);
    }`

我的通知

public void startFallNotification(String name, String loc){

    Intent intent = new Intent(); //create intent for notifcation
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);


    Notification noti = new Notification.Builder(this)
            .setVibrate(new long[]{1000,1000,1000,1000,1000}) //sets vibrate
           .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
             //sets sound
            .setTicker(name+" has fallen at "+loc)
            .setContentTitle(name+" has fallen at "+loc)
            .setContentText(n+" has fallen")
            .setSmallIcon(R.drawable.ic_launcher)
            .addAction(R.drawable.ic_launcher, "Call Now", null)
            .setContentIntent(pIntent).getNotification();

    NotificationManager notiMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notiMan.notify(0,noti);
}

【问题讨论】:

  • 您是否尝试在通知点击时调用该方法?指定您的调用层次结构和流程。
  • 我的主要目标是当有事情发生时,我会收到通知,通知中有一个按钮可以拨打电话。

标签: android android-intent notifications android-pendingintent


【解决方案1】:

您需要使用 CALL 操作创建一个 PendingIntent,并在您在 Notification 上调用 addAction() 时提供该操作。像这样:

// This activity will be started when the user clicks on the notification
Intent intent = new Intent(); //create intent for notifcation
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

// Create a PendingIntent for the CALL action
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" +emergecyNumber));
PendingIntent callPendingIntent = PendingIntent.getActivity(this, 0, callIntent, 0);


Notification noti = new Notification.Builder(this)
        .setVibrate(new long[]{1000,1000,1000,1000,1000}) //sets vibrate
       .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
         //sets sound
        .setTicker(name+" has fallen at "+loc)
        .setContentTitle(name+" has fallen at "+loc)
        .setContentText(n+" has fallen")
        .setSmallIcon(R.drawable.ic_launcher)
        .addAction(R.drawable.ic_launcher, "Call Now", callPendingIntent)
        .setContentIntent(pIntent).getNotification();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多