2

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

}
4

1 回答 1

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

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

尝试这个

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());
}
于 2015-11-01T19:54:51.540 回答