1

我正在使用 Activity 来启动服务。在服务中,我根据 locationListener 发送通知。发送通知时,用户单击通知并调用 MainActivity。

当服务调用 MainActivity 时,我想显示一个警报框。

这是Service类中的代码

public class NotificationService extends Service implements LocationListener{
     private NotificationManager mNM;
     private Notification notification;
     private int NOTIFICATION = 123;
.
.
.
        private void showNotification(String msg) {
            long[] pattern = { 0, 1000, 1000 };//pattern for the vibration
            String ns = Context.NOTIFICATION_SERVICE;
            CharSequence text ="EasyTransit notification";
            notification = new Notification(R.drawable.arrow, text, System.currentTimeMillis());
            Intent contentIntent = new Intent(this, MainActivity.class);
           // PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
            contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            notification.setLatestEventInfo(this, "EastTransit.Prepare to get off", msg, PendingIntent.getActivity(this.getBaseContext(), 0, contentIntent, PendingIntent.FLAG_CANCEL_CURRENT));

            notification.flags |= Notification.FLAG_AUTO_CANCEL;     
            //notification.flags |= Notification.FLAG_ONGOING_EVENT;
            mNM.notify(NOTIFICATION, notification);
            Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(pattern , -1);
             try {
                    Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                    Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
                    r.play();
                } catch (Exception e) {}
        }

..

我还想知道如何将字符串 msg 发送到服务调用的 Activity

我怎样才能实现这些?

4

0 回答 0