0
即時通訊嘗試設置擡頭通知,有一個按鈕,點擊按鈕時,短信將在後臺發送,或者如果實際notifaction被點擊,它會打開一個活動(這部分我可以做,沒問題)。 這是我的代碼如下,但不知道如果我做正確的方式。等待意圖通知不起作用
MainActivity:
private void sendNotifaction(){
// Create an explicit content Intent that starts the main Activity.
Intent notificationIntent = new Intent(this, sendSmsService.class);
// Construct a task stack.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Add the main Activity to the task stack as the parent.
stackBuilder.addParentStack(MainActivity.class);
// Push the content Intent onto the stack.
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack.
PendingIntent notificationPendingIntent =
stackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT);
// Get a notification builder that's compatible with platform versions >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Define the notification settings.
builder.setSmallIcon(R.mipmap.ic_launcher)
// In a real app, you may want to use a library like Volley
// to decode the Bitmap.
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher))
.setColor(Color.RED)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVibrate(new long[]{10,10,10})
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setContentTitle("Noti")
.setContentText(getString(R.string.geofence_transition_notification_text))
.addAction(R.mipmap.ic_launcher,"Send",notificationPendingIntent)
.setContentIntent(notificationPendingIntent);
// Dismiss notification once the user touches it.
builder.setAutoCancel(true);
// Get an instance of the Notification manager
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
}
其intentService sendSmsService是我想我會錯。
sendSmsService
public class sendSmsService extends IntentService {
String senderNum = "";
String sms = "";
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public sendSmsService(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(senderNum, null, sms, null, null);
Toast.makeText(getApplicationContext(), "Sms Sent", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Sms Failed", Toast.LENGTH_LONG).show();
}
}
}
它似乎犯規未燒製的代碼在手柄上的。