由GCM推送Android应用程序的通知(Push notification for android app by GCM)
我正在研究Android应用程序的事件提醒,这个应用程序只显示事件列表和有关它的详细信息..某些事件的日期是更改。
我需要推送通知以提醒用户有关活动日期的信息
阅读Google云端消息传递(GCM)后,我感到很困惑
是否有必要要求用户通过应用程序输入姓名或电子邮件? 我不需要那个!
我在哪里可以写一个新的通知“消息”来发送它?
谢谢 ,
I am working on android app for events reminder , this app only display event as list and details about it .. some event's date is change .
I need to push notification to remind a user about event date
I'am Confused when read about Google Cloud Messaging ( GCM )
Is it necessary to request from the user to enter name or email through the application ? I don't need that !
where can I write a new notification "message" to send it ?
Thank you ,
原文:https://stackoverflow.com/questions/35894906
最满意答案
你正在用另一个影子你的for-loop
i
:for i in range(1,3): # type(i) is int … for i in scan1: … # type(i) is str … diskchange[i] = disk # i is str() here, so it yells at you!
将你的内循环索引从
i
更改为j
,你应该没问题。在python中,变量的可见性超过了一个完整的函数,而不是一个块。 因此,当您在块中定义变量时,它在块之后仍然可见:
i = 'foobar' print(i, type(i)) for i in range(100): pass print(i, type(i))
会给:
foobar str 100 int
因此在重用变量名时要小心。
要解决您的问题,只需将索引名称更改为
j
:for j in scan1: if j not in scan2: result = result + j
奖金:
在下面的代码片段中,您将通过将整个内容发送到shell来读取脚本的完整内容以执行它:
with open('removeslot%d.sh' %i, 'rb') as f: script = f.read() subprocess.call(script, shell=True)
这实际上是过度设计,你可以简单地做:
subprocess.call('removeslot%d.sh' % i, shell=True)
执行脚本(或
'bash removeslot%d.sh' % i
,以防您的.sh
不可执行)。 还要注意你再次遮蔽一个符号:file
是用于处理全局可用文件的类。 改用f
!HTH
You're shadowing your for-loop
i
with anotheri
:for i in range(1,3): # type(i) is int … for i in scan1: … # type(i) is str … diskchange[i] = disk # i is str() here, so it yells at you!
Change your inner loop's index from
i
toj
and you should be fine.In python, the variable's visibility is over a full function, not a block. So when you're defining a variable within a block, it's still visible after the block:
i = 'foobar' print(i, type(i)) for i in range(100): pass print(i, type(i))
will give:
foobar str 100 int
so be careful when reusing variable names.
To fix your issue, just change the index name to
j
:for j in scan1: if j not in scan2: result = result + j
bonus:
In the following snippet, you're reading the full content of a script to execute it by sending its whole content to a shell:
with open('removeslot%d.sh' %i, 'rb') as f: script = f.read() subprocess.call(script, shell=True)
this is actually over engineered, you can simply do:
subprocess.call('removeslot%d.sh' % i, shell=True)
to execute the script (or
'bash removeslot%d.sh' % i
in case your.sh
is not executable). Also beware that you're again shadowing a symbol:file
is the class for handling files that is globally available. Usef
instead!HTH
相关问答
更多-
Python - > TypeError:list indices必须是整数,而不是str(Python -> TypeError: list indices must be integers, not str)[2024-01-04]
解释者永远不会错...... 更严重的是,您将num作为子字符串,因此它是一个字符串。 如果要将其用作字符串索引,则必须将其转换为int: num = int(posArray) # ok num is now an int print temp+"("+str(num)+")" # must use str to concat it with strings print num, type(num), posArray, type(posArray) # num is in ... -
prof是proflist的元素,而不是索引。 替代 if line[1].lower()==proflist[prof]: 同 if line[1].lower() == prof: prof is an element of proflist, not an index. Substitute if line[1].lower()==proflist[prof]: with if line[1].lower() == prof:
-
self.database是一个列表。 通过指定列表中的位置来访问列表项,例如第一个项的self.database[0]和第五个项目的self.database[4] 。 您试图将text用作列表位置,这没有任何意义。 如果要基于文本键而不是整数位置存储项目,请使用字典而不是列表。 self.database is a list. List items are accessed by specifying their position within the list, for example self.da ...
-
直方图:“TypeError,列表索引必须是整数,而不是str”(Histograms: “TypeError, list indices must be integers, not str”)[2022-08-22]
该错误是因为您正在尝试将键分配给list ,而list只能由整数list[0] , list[1]索引,依此类推。 因此, hinstogram必须是dict而不是list 确保在调用add_to_hist方法时,传递一个字典。 你可以用这种方式初始化一个字典: histogram = {} 更新 根据你的评论,你不能将[['B',1],['a',3],['n',2],['!',1]]作为参数传递给add_to_his ,因为不是一个字典。 它应该是{'B':1,'a':3,'n':2,'!':1} Th ... -
你用一个香草Python列表覆盖了你的DF: df = [['Adj. Open','Adj. High','Adj. Low','Adj. Close','Adj. Volume']] 试试这个: In [102]: df = df.filter(regex='^Adj\.') In [103]: df Out[103]: Adj. Open Adj. High Adj. Low Adj. Close Adj. Volume Date 2004-08-19 ...
-
只需改变需要做的事情就是必须将features初始化为dict ( {} )而不是list ( [] ),然后才能填充其内容。 TypeError是因为word_features是您尝试使用列表进行索引的字符串列表,列表不能包含字符串索引。 features={} for w in word_features: features[w] = (w in words) 这里, word_features存在的元素构成字典的keys ,保存布尔值的特征,基于相同元素是否出现在words (其中由于调用s ...
-
Python3 TypeError:列表索引必须是整数或切片,而不是str(Python3 TypeError: list indices must be integers or slices, not str)[2022-02-17]
首先,在内置python语句或数据结构(如list , tuple或引用的模块的名称)之后,不要将变量命名,这也适用于文件。 例如命名你的文件socket.py并导入socket模块肯定会导致错误(我会让你自己尝试) 在你的代码element是一个字符串,迭代的索引必须是数字而不是字符串,所以你可以告诉python 给我2号位的物品。 但是现在你试着说给我A位置的项目,这在英语中是无效的,没有谈论编程语言。 你应该使用enumerate函数,如果你想循环迭代的时候获得迭代的索引,或者你可以这样做 for i ... -
你正在用另一个影子你的for-loop i : for i in range(1,3): # type(i) is int … for i in scan1: … # type(i) is str … diskchange[i] = disk # i is str() here, so it yells at you! 将你的内循环索引从i更改为j ,你应该没问题。 在python中,变量的可见性超过了一个完整的函数,而不是一个块。 因此,当 ...
-
这条线: res = sorted(res) 并没有回归你的想法。 在字典上使用sort将对其键进行排序并将其作为列表返回。 当您在上下文管理器中执行res[key] ,您将使用字符串为列表编制索引,从而导致错误。 如果您想在字典中订购,可以通过以下两种方式之一进行订购: 重命名您创建的列表: sorted_keys = sorted(res) 然后在索引静止引用dict名称res同时迭代这些。 或者,使用OrderedDict ,然后像使用普通字典一样遍历其成员: from collections i ...
-
TypeError:列表索引必须是整数,而不是Flask App中的str(TypeError: list indices must be integers, not str in Flask App)[2022-09-27]
您有一个包含字典的元组列表。 无法使用字符串访问,您需要使用整数。 最初的Django应用程序使用了一种名为SortedDict的特殊类型的Django字典,幸好Python自2.7以来就有一个名为OrderedDict的等价字典。 你只需要像这样导入它 from collections import OrderedDict 然后调整语法以使用OrderedDict并删除值周围的_() : meters_info = OrderedDict([ ("instance", { ...