Từ thử nghiệm của mình, tôi không thể tìm được cách tốt để làm điều này, và thay vào đó là giải pháp không nhất thiết phải là 'thực hành tốt nhất'. Nó chỉ hoạt động với ứng dụng twitter chính thức chứ không phải với các ứng dụng khác. Giải pháp này sẽ thất bại nếu ứng dụng chính thức thay đổi API nội bộ của nó. Vì vậy, hãy thận trọng khi sử dụng giải pháp này và biết những hạn chế của nó.
Mã này không được viết theo cách tốt, nhưng nó hoạt động. Lời khuyên của tôi là thay đổi nó để không sử dụng quá nhiều tài nguyên.
Mã kiểm tra xem liệu ứng dụng Twitter đã được cài đặt chưa. Nếu có, ứng dụng Twitter sẽ được khởi chạy; nếu không, trình duyệt được khởi chạy.
Twitter có tên twitter (cũng tên là screen_name) và id twitter: chúng không giống nhau.
//Checking If the app is installed, according to the package name
Intent intent = new Intent();
intent.setType("text/plain");
intent.setAction(Intent.ACTION_SEND);
final PackageManager packageManager = getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : list)
{
String packageName = resolveInfo.activityInfo.packageName;
//In case that the app is installed, lunch it.
if (packageName != null && packageName.equals("com.twitter.android"))
{
try
{
String formattedTwitterAddress = "twitter://user/" ;
Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress));
long twitterId = <Here is the place for the twitter id>
browseTwitter.putExtra("user_id", twitterId);
startActivity(browseTwitter);
return;
}
catch (Exception e)
{
}
}
}
//If it gets here it means that the twitter app is not installed. Therefor, lunch the browser.
try
{
String twitterName = <Put the twitter name here>
String formattedTwitterAddress = "http://twitter.com/" + twitterName;
Intent browseTwitter = new Intent(Intent.ACTION_VIEW, Uri.parse(formattedTwitterAddress));
startActivity(browseTwitter);
}
catch (Exception e)
{
}
ok..so bối cảnh ở đây là gì? – AutoMEta
No..tweetIntetnt is not working .. Tôi nhận được điều này khi tôi bắt đầu này. hoàn thành hành động bằng cách gửi qua Bluetooth, Dropbox, Gmail – AutoMEta
Kiểm tra nguồn thứ 2 để biết thêm thông tin về tweetIntent –