2016-11-09 52 views
1

我开始一个显示通知的前台服务。如果我的活动被隐藏,我希望它通过点击通知开始。 称为onStartCommand一个功能做到这一点:android:如何通过点击前台服务通知来调用活动

startForeground(noti_id, mNoti); 

通知显示和工作,但它并没有激活我的MainActivity:

notiIntent = new Intent(this, MainGate.class); 
notiPendingIntent = PendingIntent.getActivity(this, 0, notiIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

MainGate.class是启动前台服务活动。它应该出现在我点击通知时。

编辑:

其实,它的工作时,通知始建于人的活动(MainGate.class)。当通知在服务中建立而不是前台服务时,它就起作用了。现在我必须执行前台服务并停止工作。

回答

0

尝试此解决方案

Intent notificationIntent = new Intent(this, MainGate.class); 
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, 
     notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 

Notification notification=new NotificationCompat.Builder(this) 
          .setSmallIcon(R.drawable.ic_launcher) 
          .setContentText(getString(R.string.isRecording)) 
          .setContentIntent(pendingIntent).build(); 

startForeground(NOTIFICATION_ID, notification); 
+0

对不起,不能正常工作。 – user3137385

相关问题