【发布时间】:2019-01-03 10:32:07
【问题描述】:
我正在尝试为“其他人”创建 facebook 在应用程序上打开,而不是在浏览器上打开,例如 National Geography https://www.facebook.com/natgeo
这是我的代码:
ddc.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/natgeo"));
startActivity(browserIntent);
}
});
我在stackoverflow上找到了一个,我试过了,它有错误,这里是代码:
try {
//try to open page in facebook native app.
String uri = "fb://page/" + natgeo; //Custom URL
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
catch (ActivityNotFoundException ex){
//facebook native app isn't available, use browser.
String uri = "https://www.facebook.com/natgeo" + natgeo; //Normal URL
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uriMobile));
startActivity(i);
}
事情是我想要的,如果有人没有facebook App,它会在浏览器中打开。
编辑
package fa;
import com.f.fa.R;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button ddc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
addListenerOnButton();
}
public void addListenerOnButton() {
ddc = (Button) findViewById(R.id.ddc);
try {
//try to open page in facebook native app.
String uri = "fb://page/" + natgeo; //Custom URL
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
catch (ActivityNotFoundException ex){
//facebook native app isn't available, use browser.
String uri = "https://www.facebook.com/natgeo" + natgeo; //Normal URL
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
}
}
}
【问题讨论】:
-
您遇到了哪些错误?
-
"natgeo" 有一个错误认为它应该去那里(我不知道).. "uriMobile" 是错误的,我不知道他们想要这个号码
-
请解释得更清楚...如果您遇到异常,请告诉我们。如果您认为变量的内容有误,请调试它。试试我的回答中的建议