我有一个问题。当我调用 finish() 方法时, Activity 仍然保留在任务管理器中,如果用户从任务管理器重新启动它,我的 Activity 会收到旧的 Intent 。如果该 Intent 是从推送通知发送的,我会有不希望的 react :我的应用程序使用推送通知数据启动流程 Intent 。
如何正确管理我的 Activity 中的推送通知 Intent 行为以避免错误的 Activity 状态?
我的应用收到推送通知并形成待处理的推送响应 Intent :
final NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int defaultOptions = Notification.DEFAULT_LIGHTS;
defaultOptions |= Notification.DEFAULT_SOUND;
Intent intentAction = new Intent(context, MainActivity.class);
intentAction.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intentAction.putExtra(BUNDLE_COLLAPSE_KEY, data.getString(BUNDLE_COLLAPSE_KEY));
intentAction.setAction(CUSTOM_ACTION);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intentAction, PendingIntent.FLAG_UPDATE_CURRENT);
int notificationFlags = Notification.FLAG_AUTO_CANCEL;
final Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.icon_splash)
.setContentTitle(context.getResources().getString(R.string.app_name))
.setContentText(data.getString(BUNDLE_PUSH_CONTENT_DATA))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setDefaults(defaultOptions)
.getNotification();
notification.flags |= notificationFlags;
mNotificationManager.notify(NOTIFICATION_ID, notification);
用户通过推送进入应用程序后,应用程序显然会通过 CUSTOM_ACTION 接收 Intent 并执行一些工作:
private void intentProcess(Intent intent) {
boolean customAction = intent.getAction().equals(GCMPushReceiver.CUSTOM_ACTION);
if (customAction) {
//push reaction, do some staff with intent
} else {
//no push reaction, user just open activity
}
}
我从 onCreate 和 onNewIntent 调用 intentProcess 方法:
public class MainActivity extends FragmentActivity {
//this case if my app closed and user tap on push
@Override
protected void onCreate(Bundle savedInstanceState) {
/* ... */
intentProcess(getIntent());
}
//this case if my app opened and user tap on push
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
intentProcess(intent);
}
}
list 中的 Activity 声明:
<activity
android:name=".ui.activity.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
最佳答案
尝试使用它
int id= NotificationID.getID();
final PendingIntent pendingIntent = PendingIntent.getActivity(context, id, intentAction, PendingIntent.FLAG_UPDATE_CURRENT);
// rest of your code
notificationManager.notify(id, notification);
添加一个新类
public class NotificationID {
/**
* An AtomicInteger is used in applications such as atomically incremented
* counters, and cannot be used as a replacement for an Integer. However,
* this class does extend Number to allow uniform access by tools and
* utilities that deal with numerically-based classes.
*/
private final static AtomicInteger c = new AtomicInteger(0);
/**
* Method to the automatically incremented int value.
*
* @return
*/
public static int getID() {
return c.incrementAndGet();
}
}
每次生成通知时,它都会创建具有不同 ID 的新 Intent 。
如果您不希望您的 Activity 显示/添加到任务管理器,请在 AndroidManifest 的 Activity 标签中添加这些行。
android:excludeFromRecents="true"
android:noHistory="true"
关于Android - Intent 管理。如果用户从任务管理器打开应用程序,则重新发送旧 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25357904/
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我正在使用i18n从头开始构建一个多语言网络应用程序,虽然我自己可以处理一大堆yml文件,但我说的语言(非常)有限,最终我想寻求外部帮助帮助。我想知道这里是否有人在使用UI插件/gem(与django上的django-rosetta不同)来处理多个翻译器,其中一些翻译器不愿意或无法处理存储库中的100多个文件,处理语言数据。谢谢&问候,安德拉斯(如果您已经在rubyonrails-talk上遇到了这个问题,我们深表歉意) 最佳答案 有一个rails3branchofthetolkgem在github上。您可以通过在Gemfi
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
如何使用RSpec::Core::RakeTask初始化RSpecRake任务?require'rspec/core/rake_task'RSpec::Core::RakeTask.newdo|t|#whatdoIputinhere?endInitialize函数记录在http://rubydoc.info/github/rspec/rspec-core/RSpec/Core/RakeTask#initialize-instance_method没有很好的记录;它只是说:-(RakeTask)initialize(*args,&task_block)AnewinstanceofRake
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
如果我使用ruby版本2.5.1和Rails版本2.3.18会怎样?我有基于rails2.3.18和ruby1.9.2p320构建的rails应用程序,我只想升级ruby的版本,而不是rails,这可能吗?我必须面对哪些挑战? 最佳答案 GitHub维护apublicfork它有针对旧Rails版本的分支,有各种变化,它们一直在运行。有一段时间,他们在较新的Ruby版本上运行较旧的Rails版本,而不是最初支持的版本,因此您可能会发现一些关于需要向后移植的有用提示。不过,他们现在已经有几年没有使用2.3了,所以充其量只能让更
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送