您的位置:首页 > 移动开发

Android实现外部链接唤醒App,并且在App内部打开详情页面

2018-03-15 11:07 531 查看
最近项目中遇到了这个需求,下面详情描述下,在此做个笔记。
   需求:app分享出去的链接(只有报名和投票,在详情页里面点击报名或者投票才唤起app),如果本地安装了这个应用则可以直接唤醒app并在app内部打开这个链接的详情页面,如果本地没有安装这个app,则跳转到下载页面提示去下载。
下面是具体的实现过程:
1.Mainfest中的配置
<activity android:name="com.palmnewsclient.events.OutsideActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<!-- 下面所设置的质需要和html端对调 -->
<!-- 在data里设置了 scheme和host,则该Activity可以接收和处理类似于 "sharetest://data/XXX"的链接 -->
<data
android:host="com.newnet.hlxc.palmNews"
android:scheme="scheme" />
</intent-filter>
</activity>
注:OutsideActivity是通过Manifest中的配置来启动的。
需要h5传过来的信息scheme://com.newnet.hlxc.palmNews?newType=7&contentId=12345    scheme是协议,
com.newnet.hlxc.palmNews是host值,后面的全是所需要的参数。
2.OutsideActivity关键代码
String data = getIntent().getDataString();//接收到网页传过来的数据:sharetest://data/http://www.huxiu.com/
if (data != null) {
String[] split = data.split(getPackageName() + "?");//以data/切割data字符串
String UrlData = split[1]; //就得到:http://www.huxiu.com/(这就是我们需要网页传给我们的数据)
String[] param = UrlData.split("&");
contentId = Integer.parseInt((param[0].split("="))[1]);
newType = Integer.parseInt((param[1].split("="))[1]);
} else {
text.setText("参数错误,无法启动");
return;
}
注:
String data = getIntent().getDataString();//接收到网页传过来的数据:scheme://com.newnet.hlxc.palmNews?newType=7&contentId=12345
3.OutsideActivity类的具体实现代码:import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.newnet.hlxc.palmNews.R;
import com.palmnewsclient.MainActivity;
import com.palmnewsclient.base.BaseActivity;
import com.palmnewsclient.bean.NewsListBean;
import com.palmnewsclient.data.AppConfig;
import com.palmnewsclient.http.rx.RxBus;
import com.palmnewsclient.http.subscriber.SimpleSubscriber;
import com.palmnewsclient.newcenter.OtherNewsTypeActivity;
import com.palmnewsclient.newcenter.VoteActivity;
import com.palmnewsclient.newcenter.helper.NewHelpUtils;
import com.palmnewsclient.usercenter.LoginActivity;
import com.palmnewsclient.utils.AppManager;
import com.palmnewsclient.utils.Constants;
import com.palmnewsclient.utils.SPUtils;

import static com.palmnewsclient.utils.SPUtils.getBooleanType;

/**
* 外部启动Activity
* 描述:该类实现的需求是报名和投票类
* 点击外部分享链接启动本地App并跳转详细页面
*/
public class OutsideActivity extends BaseActivity {

private int newType;
private int contentId;
private String tooken;
private TextView text;
private String url;
private NewsListBean.BodyEntity.DataEntity bean;

@Override
protected int initRootView() {
return R.layout.activity_outside;
}

@Override
protected void initTitle() {
//外部绑定成功
RxBus.getDefault().toObservable(OutSideBindSucceed.class).subscribe(new SimpleSubscriber<OutSideBindSucceed>() {
@Override
public void onNext(OutSideBindSucceed imageController) {
if (imageController.isSuccess()) {
openActivity();
}
}
});
//外部登录成功
RxBus.getDefault().toObservable(OutSideEvent.class).subscribe(new SimpleSubscriber<OutSideEvent>() {
@Override
public void onNext(OutSideEvent event) {
if (event.getOutside()) {
openActivity();
}
}
});
}

@Override
protected void initViews() {
text = (TextView) findViewById(R.id.text);
}

@Override
protected void initData() {
String data = getIntent().getDataString();//接收到网页传过来的数据:sharetest://data/http://www.huxiu.com/

if (data != null) {
String[] split = data.split(getPackageName() + "?");//以data/切割data字符串
String UrlData = split[1]; //就得到:http://www.huxiu.com/(这就是我们需要网页传给我们的数据)
String[] param = UrlData.split("&");
contentId = Integer.parseInt((param[0].split("="))[1]);
newType = Integer.parseInt((param[1].split("="))[1]);
Log.e("WEIWEI", "newType=" + newType + " contentId=" + contentId);
Log.e("WEIWEI", data);
} else {
text.setText("参数错误,无法启动");
return;
}
boolean islogin = getBooleanType(this, Constants.USER_LOGIN_STATUS);
boolean isBindMobile = getBooleanType(this, Constants.USER_LOGIN_THIRD_WAY_BIND_MOBILE_STATUS);
boolean isthirdLogin = SPUtils.getBooleanType(this, Constants.USER_LOGIN_THIRD_WAY_STATUS);
Log.e("WEI", "isLogin==" + islogin + "");
if (islogin) {
if (!isthirdLogin) {
openActivity();
Log.e("TAG", "登陆的状态下=========================");
} else {
if (isBindMobile) {
Log.e("TAG", "已绑定状态下=========================");
openActivity();
} else {
Log.e("TAG", "未绑定状态下=========================");
Bundle b = new Bundle();
b.putCharSequence(AppConfig.SHOW_BIND_DIALOG, "bind");
AppManager.getInstance().jumpActivity(this, MainActivity.class, b);
AppManager.getInstance().finishActivity(this);

}
}

} else {
Log.e("TAG", "未登陆的状态下=========================");
Bundle bundle = new Bundle();
bundle.putString("OUT_SIDE", "out_side");
AppManager.getInstance().jumpActivity(this, LoginActivity.class, bundle);
AppManager.getInstance().finishActivity(this);
}
}

@Override
protected void initListener() {

}

@Override
protected void onResume() {
super.onResume();
Log.e("TAG", "只走了onResume=========================");
}

private void openActivity() {
getBundle();
AppManager.getInstance().jumpActivity(this, MainActivity.class, null);
if (newType == 8) {
AppManager.getInstance().jumpActivity(this, VoteActivity.class, bundle);
} else {
AppManager.getInstance().jumpActivity(this, OtherNewsTypeActivity.class, bundle);
}
AppManager.getInstance().finishActivity(this);
}

private Bundle bundle;

@NonNull
private void getBundle() {
tooken = SPUtils.getStringType(this, Constants.USER_LOGIN_TOKEN);
url = NewHelpUtils.getNewsUrlByNewsType(this, newType, contentId, tooken);
Log.e("WEIWEI", url);
bean = new NewsListBean.BodyEntity.DataEntity();
bean.setTitle(NewHelpUtils.getNewsTitleByNewsType(newType));
bean.setNewType(newType);
bean.setContentId(contentId);
bean.setId(contentId);
Intent intent = new Intent();
intent.putExtra(Constants.NEW_DETAIL_TITLE, NewHelpUtils.getNewsTitleByNewsType(newType));
intent.putExtra(Constants.NEW_DETAIL_SHARE_BEAN, bean);
intent.put
b05f
Extra(Constants.NEW_DETAIL_URL, url);
bundle = new Bundle();
bundle.putString(Constants.NEW_DETAIL_URL, url);//路径
bundle.putString(Constants.NEW_DETAIL_TITLE, NewHelpUtils.getNewsTitleByNewsType(newType));//文章详情标题
bundle.putString(Constants.NEW_LINK_TITLE, NewHelpUtils.getNewsTitleByNewsType(newType));//文章链接标题
bundle.putSerializable(Constants.NEW_DETAIL_SHARE_BEAN, bean);
}

@Override
public void onClick(View v) {

}
}参考博文:点击打开链接 http://blog.csdn.net/starry_eve/article/details/52873485
                点击打开链接 http://blog.csdn.net/daijin888888/article/details/50009387

                点击打开链接 https://www.cnblogs.com/alex-mh/p/7040736.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐