如何使用通知addAction调用方法或电话号码

时间:2014-11-04 08:19:48

标签: android android-intent notifications android-pendingintent

我试图调用此方法。或者更重要的是,当我按下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);
}

1 个答案:

答案 0 :(得分:1)

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

// 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();