android - 如何发出调用号码的通知?

标签 android android-intent notifications android-phone-call

我希望它在单击屏幕上的“通知”按钮后创建一个 pendingIntent 通知。因此,当用户单击通知时,它将调用 021-1234567 等号码。我该怎么做?

我一直在网上寻求帮助,但似乎找不到与此相关的任何内容。

public void notifyPendingIntent(View view) {

    Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+ 0211234567));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

    PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    // Build notification
    Notification notify = new Notification.Builder(this)
            .setContentTitle("Make this call")
            .setContentText("New call must be made to 021 123 4567")
            .setTicker("New Alert")
            .setContentIntent(pIntent).build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    // hide the notification after its selected
    notify.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, notify);
}

到目前为止我已经做到了,但是当我按下按钮时,没有任何反应。

我试过了,现在可以了。感谢所有试图提供帮助的人。

public void notifyPendingIntent(View view) {

    NotificationCompat.Builder myNotification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.my_notification_icon)
            .setContentText("Calling 021-12345678")
            .setContentTitle("Phone Call Notification");

    Intent phoneCall = new Intent(Intent.ACTION_CALL);
    phoneCall.setData(Uri.parse("tel:021-12345678"));
    PendingIntent phoneCallIntent = PendingIntent.getActivity(this, 0, phoneCall, PendingIntent.FLAG_UPDATE_CURRENT);

    myNotification.setContentIntent(phoneCallIntent);

    NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(0, myNotification.build());

}

最佳答案

<!-- NOTE! Your uses-permission must be outside the "application" tag
           but within the "manifest" tag. -->

<uses-permission android:name="android.permission.CALL_PHONE" />

Try this

public void call() {
    Toast.makeText(this,"call",Toast.LENGTH_SHORT).show();
    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel: 0211234567"));
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, callIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(getResources().getString(R.string.app_name))
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);


    NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
    notificationBuilder.setStyle(inboxStyle);
    inboxStyle.setBigContentTitle("sdjshfjnds");
    inboxStyle.addLine("sdjjsdfn");
    notificationBuilder.setStyle(inboxStyle);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int NOTIFICATION_ID = 100;
    notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}

关于android - 如何发出调用号码的通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33466329/

相关文章:

android - 创建带有到期日期的 Android 通知

java - Android 中带有详细信息 View 的 ListView

android - Intent.putExtras 不一致

android - 如何使用来自 Google 的 OAuth 回调来避免 "Complete action with"弹出窗口?

java - 片段中的按钮导致 Activity 无法正常工作(已更新)

swift - AVPlayer 的 addPeriodicTimeObserver 函数未正确执行

android - JQueryMobile 和信用卡输入

android - 获取当前 fragment

android - 需要 RXJava 异步帮助。输入被调用阻塞

swift - 如何将 Realm 通知抽象为带有闭包的通用结构?