简体   繁体   English

Android通知setContentView

[英]Android Notification setContentView

http://developer.android.com/guide/topics/ui/notifiers/notifications.html Page says that, I have to use Intent http://developer.android.com/guide/topics/ui/notifiers/notifications.html Page说,我必须使用Intent

I can't use Intent because my code have only one Activity. 我无法使用Intent,因为我的代码只有一个Activity。 I want to use notification to get back to app eg from DEVICE'S HOME SCREEN . 我想使用通知回到应用程序,例如从DEVICE'S HOME SCREEN

I have to use Intents when I want to execute something by clicking the notification. 当我想通过单击通知执行某些操作时,我必须使用Intent。 Is there a way to use setContentView after clicking the notification? 有没有办法在单击通知后使用setContentView

Without seeing what you have attempted so far, I am going to take a stab at this one. 如果没看到你到目前为止所尝试的内容,我将会对此进行一次尝试。

I don't quite see why regardless of how many Activities you have in your app, you cannot declare / use an Intent . 我不太明白为什么无论您的应用程序中有多少活动,您都无法声明/使用 Intent If you need to trigger your only Activity again, after a Notification has been clicked, and need to call the setContentView(R.layout.some_layout_xml); 如果您需要再次触发您的Activity,则在单击Notification之后,需要调用setContentView(R.layout.some_layout_xml); in the Activities onCreate() again, why not declare an Intent for your Notification like this: 再次在Activities onCreate() ,为什么不为你的Notification声明一个Intent,如下所示:

Intent intent = new Intent(getApplicationContext(), YOUR_ACTIVITY.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(YOUR_ACTIVITY.this, 0, intent, 0);

This way, clicking on your Notification should do what you want it to do. 这样,单击您的通知应该按照您的意愿执行。

Again, this may or may not work for you considering that you haven't posted any code at all. 同样,考虑到您根本没有发布任何代码,这可能适用于您,也可能不适合您。

EDIT: Based on the comments by the OP, what I think the solution maybe: 编辑:基于OP的评论,我认为解决方案可能是:

Refer to this link on how to send data with an Intent for a Notification: How to send parameters from a notification-click to an activity? 请参阅此链接,了解如何使用Intent发送通知数据: 如何从通知点击发送参数到活动?

Basically, since a Notification has been triggered, I am assuming that the user has already logged in. Now to skip the login part when they come back to the app after clicking a Notification , send some extras along with the Intent . 基本上,由于已经触发了Notification ,我假设用户已经登录。现在,当他们在点击Notification后返回应用程序时跳过登录部分,发送一些额外内容和Intent Then, when the Activity starts, check for the value and using an if...else statement, decide which layout should be shown. 然后,当Activity启动时,检查值并使用if...else语句,决定应显示哪个布局。

Again, and I cannot stress this enough, you should always show what you have done so far. 再说一遍,我不能强调这一点,你应该总是展示你到目前为止所做的事情。 That really helps finding a solution. 这真的有助于找到解决方案。 Your actual requirement has no bearing on what you need done at all. 您的实际要求与您需要完成的工作无关。

You can use an Intent to get back to the app, eg like this: 您可以使用Intent返回应用程序,例如:

final Intent intent = new Intent(context, YourActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(
                context, 0, intent, 0);
final Notification notification = new Notification(
                R.drawable.logo, tickerText, System.currentTimeMillis());
notification.setLatestEventInfo(context, title, text, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM