通过使用意图从Android应用程序推特 [英] Tweeting by using intent from android app
问题描述
我用下面的code鸣叫从另一个应用程序的内容:
I'm using the following code to tweet something from another app:
try{
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "It's a Tweet!" + "#MyApp");
intent.setType("text/plain");
final PackageManager pm = getPackageManager();
final List<?> activityList = pm.queryIntentActivities(intent, 0);
int len = activityList.size();
for (int i = 0; i < len; i++) {
final ResolveInfo app = (ResolveInfo) activityList.get(i);
if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
final ActivityInfo activity=app.activityInfo;
final ComponentName x=new ComponentName(activity.applicationInfo.packageName, activity.name);
intent=new Intent(Intent.ACTION_SEND);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.setComponent(x);
intent.putExtra(Intent.EXTRA_TEXT, "blah blah" );
startActivity(intent);
break;
}
}
} catch(final ActivityNotFoundException e) {
Log.i("Twitter intent", "no twitter native", e );
}
以上code完美的作品,如果Twitter的应用程序安装在手机上,但没有打开。但是,如果我使用Twitter使用上述活动之前,我只是单纯地得到转移到同一个点,我最后离开了Twitter和,我想鸣叫不贴文。我应该怎么做才能解决这个?
The above code works perfectly if the twitter app is installed on the phone but is not open. But if I was using twitter before using the above activity, then I just simply get transferred to the same point where I last left twitter and the text that I wanted to tweet is not posted. What should I do to correct this?
另外,如果Twitter是不是我的手机上安装,然后什么也没有发生。在pressing启动的意图上面什么也没有发生,但我不应该实际收到消息的按钮 - 没有Twitter的本地人。应该怎样做才能得到的消息??
Also, if twitter is not installed on my phone, then nothing happens. On pressing the button that starts the intent above nothing happens but shouldn't I actually be getting the message - no twitter native. What should be done to get the message??
推荐答案
我认为你应该使用,而不是setComponent setClassName。
I think you should use setClassName instead of setComponent.
intent.setClassName("com.twitter.android", "com.twitter.android.PostActivity");
但作为一个有益的补充说明,有几个选项供您与Twitter集成:
But as a helpful side note, There are several options for you to integrate with twitter:
1)使用他们的API(也许使用的库如 twitter4j ),或
1)Use their API (maybe use a library such as twitter4j), or
2)找到一个方法来与Twitter的应用程序集成,就像你现在正在这样做。这不是一个好主意,因为很多人使用其他应用程序,如羽,TweetDeck的等等,你的应用程序不会工作,如果他们没有安装叽叽喳喳。
2)Find a way to integrate with the Twitter app, like you are doing right now. This is not a good idea because many people use other apps such as Plume, TweetDeck and so on and your app wont work if they dont have twitter installed.
3)你可以打开 http://twitter.com/?status= 使用简单的意图。 这可以像来完成:
3)you can just open up http://twitter.com/?status= using a simple intent. This can be done like:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://twitter.com/?status=" + Uri.encode(message)));
startActivity(i);
您还可以结合2和3。你可以尝试一些已知Twitter的应用服务,如果他们不存在,调用浏览器。
You can also combine 2 and 3. You can try a few known twitter apps and if they are absent, call the browser.
还要注意,有可能对方法3来实际启动而不是浏览器的应用程序(或至少给用户两者之间的选择),如果该应用处理的正确的意图。
Also note that it might be possible for method 3 to actually launch an app instead of the browser (or at least give the user a choice between the two), if the app handles the correct intents.
在我看来,方法1是最好的,因为它不仅取决于你的应用程序(当然,除了库),你不必与该用户可能会或可能不会有第三方的应用程序集成。
In my opinion, Method 1 is best since it only depends on your app ( well, except for the library) and you don't have to integrate with third party apps that the user may or may not have.
这篇关于通过使用意图从Android应用程序推特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!