After I've implemented a solution for the Android app to post to web server and validate Google Order then issue a download link. Now I am trying to work on a code for the App to read the link from the thankyou.php page
Download File
The file is a ".DAT" extension and coming from a certain link. The App should retrieve the link in a new page and let the customer download the file.
Inside you manifest file you need to add an intent filter:
<activity
android:name="Downloader">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="domain.com"
android:scheme="http" />
</intent-filter>
</activity>
Then inside your "Download" activity's onCreate:
public class Download extends Activity {
private String filename;
#Override
protected void onCreate (final Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(<your layout file>);
final Intent intent = getIntent ();
final Uri data = intent.getData ();
if (data != null) {
final List<String> pathSegments = data.getPathSegments ();
fileName = pathSegments.get (pathSegments.size () - 1);
}
}
Then in the clickhandler for the download button you can use a view intent to act like a link in android.
button.setOnClickListener (new View.OnClickListener () {
#Override public void onClick (final View v) {
Uri intentUri = Uri.parse("http://domain.com/" + filename);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(intentUri);
startActivity(intent);
}
});
Related
I added App link assistant in my app to open particular activity from an external link. Now I've the following code in my DataDetailActivity.
Intent appLinkIntent = getIntent();
String appLinkAction = appLinkIntent.getAction();
Uri appLinkData = appLinkIntent.getData();
if(appLinkData != null)
{
String dataId = appLinkData.getLastPathSegment();
Intent resultIntent = new Intent(this, DataDetailActivity.class);
startActivity(resultIntent);
}
I would like to open particular DataDetailActivity. But this does throws error. Where do I have to pass dataId in the intent?
Note: We have Default HomeScreen in the app that has Login To FB button. So Do I have to write down this code in HomeScreenActivity and pass intent to DatadetailAcvity.java? If yes then how Do I tell to DataDetailScreen for particular data(If i have id)?
Any help would be appreciated.
manifests.xml
<application
....
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".HomeScreenActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="somescheme" /> <!--This line to define schema -->
</intent-filter>
</activity>
....
</application>
HomeScreenActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
checkDeepLink();
}
private void checkDeepLink(){
if (getIntent() != null && getIntent().getData() != null) {
Uri data = getIntent().getData();
String scheme = data.getScheme();
String host = data.getHost();
String param = data.getQuery();
Log.d("DeepLink","Schema : " + scheme);
Log.d("DeepLink","Host : " + host);
Log.d("DeepLink","param : " + host);
if (host.equals("page_details")){
Intent intent = new Intent(this,DatadetailAcvity.class);
intent.putExtra("detail_id",Long.valueOf(data.getQueryParameter("detail_id"))); // URL query values as string, you need to parse string to long.
startActivity(intent);
}else{
// ... other logic
}
}
}
DeepLink
scheme://host?pama_name=value&other_param_name=value
Example:
somescheme://page_details?detail_id=2
Facebook Step-by-Step Guide
Update
DatadetailAcvity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_detail);
if (getIntent() !=null) {
long detailId = getIntent().getLongExtra("detail_id",-1);
if (detailId != -1){
// do your stuff and displayed page by id
}
}
}
Without error info,but if you want pass data through deep linking, you could use query parameters.
Suppose url is https://www.example.com/example?param1=hello
Then
Intent intent = getIntent();
Uri data = intent.getData();
String param1 = data.getQueryParameter("param1");
I made a android webview app for our website. Now i want to add deep links to the app, like when somebody clicks a link of our website, you can open it with the webview app instead of chrome web browser. I added these to my manifest:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"
android:host="www.example.com" />
Now it opens the webview app when I click a link which is related to us, but it doesnt open the right page, its just starting the app again. But I want to open the directly the right page inside the webview app.
EDIT:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//added
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
But it seems that the variables action and data are not used.
I load the webview like this (for three languages).
//loads the main website
//sets the matching language
web2.clearCache(true);
String loc = Locale.getDefault().toString();
if (loc.startsWith("de")) {
web2.loadUrl("https://www.example.de");
} else if (loc.startsWith("nl")) {
web2.loadUrl("https://www.example.nl");
} else {
web2.loadUrl("https://www.example.com");
}
You must use the intent data that your activity receives when its opened via deep link. Example in Kotlin:
class WebViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_webview)
val uri = intent.data
webView.loadUrl(uri.toString())
}
Now I did it like this. That is the solution.
Intent intent = getIntent();
Uri data = intent.getData();
String loc = Locale.getDefault().toString();
if (data==null && loc.startsWith("de")) {
web2.loadUrl("https://www.example.de");
} else if (data==null && loc.startsWith("nl")) {
web2.loadUrl("https://www.example.nl");
} else if (data==null && !loc.startsWith("de") && !loc.startsWith("nl")){
web2.loadUrl("https://www.example.com");
} else {
web2.loadUrl(data.toString());
}
I have a blog and I made a app version of it using just Webview. The problem is: I would like to share my posts through other apps, like sending a whatsapp message or an email to someone with a link to a specific post located on my blog (like www.blogspot.myblog.com/2017/postexample), and then have the option to open this with my app, like Facebook does, or even Youtube does.
I made this:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="mywebsite.com"
android:pathPrefix="/"
android:scheme="http" />
<data
android:host="www.mywebsite.com"
android:pathPrefix="/"
android:scheme="http" />
</intent-filter>
it does launch my app, but (of course) it opens to the app's default page, not the www.blogspot.myblog.com/2017/postexample, so what should I do to open directly into the sent url? Thanks guys, a help would be greatly appreciated.
EDIT
Here's my code now:
<intent-filter >
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http"
android:host="www.blog.com"
android:pathPrefix="/" />
</intent-filter>
</activity>
And the Activity
public class BlogActivity extends AppCompatActivity {
private static final String TAG = "BlogActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
//Get the link from that Uri
if (data != null) {
Log.d(TAG, "Data" + data.toString());
WebView WebView2 = (WebView)findViewById(R.id.web1);
WebView2.setWebViewClient(new WebViewClient());
WebView2.loadData(TAG, "text/html; charset=utf-8", "UTF-8");
WebSettings webSettings = WebView2.getSettings();
webSettings.setJavaScriptEnabled(true);
}
}
}
Have a look at: https://developer.android.com/training/app-indexing/deep-linking.html
From the above link:
Once the system starts your activity through an intent filter, you can use data provided by the Intent to determine what you need to render. Call the getData() and getAction() methods to retrieve the data and action associated with the incoming Intent. You can call these methods at any time during the lifecycle of the activity, but you should generally do so during early callbacks such as onCreate() or onStart().
Here’s a snippet that shows how to retrieve data from an Intent:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
//Get the link from that Uri
if(data != null) {
Log.d(TAG, "Data" + data.toString());
}
}
I have an application named as "App" and another application named as "App1", I have a button in "App" when I click that button I want to open "App1", for this I am using Intent but it does not open "App1".Please help
here is my code for button in "App":-
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app);
init();
}
public void init(){
mTranslucentBtn = (Button) findViewById(R.id.button);
mTranslucentBtn.setAlpha(0.7f);
mTranslucentBtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v){//calling an activity using <intent-filter> action name
startNewActivity(MainActivity.this,"com.example.devui1.rewardapp");
}
});
}
public void startNewActivity(Context context, String packageName) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
if (intent == null) {
// Bring user to the market or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + packageName));
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
In order for getLaunchIntentForPackage() to work, that package needs to have an Activity that is suitable for being launched as an entry point into that app. The documentation includes this:
The current implementation looks first for a main activity in the category CATEGORY_INFO, and next for a main activity in the category CATEGORY_LAUNCHER. Returns null if neither are found.
This suggests you need one of the following on an Activity in your other package:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.INFO" />
</intent-filter>
Or
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
If that's not something you want, then you could try creating your own custom action string and using that to launch an activity exposing that specific action:
<intent-filter>
<action android:name="your.package.ACTION_NAME" />
</intent-filter>
public void startActivityWithPrivateAction(Context context, String packageName) {
Intent intent = new Intent("your.package.ACTION_NAME");
intent.setPackage(packageName);
List<ResolveInfo> activities = context.getPackageManager().queryIntentActivities(intent, 0);
if (activities.isEmpty() {
// no suitable activity was found; open the market instead.
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + packageName));
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
I've already enabled deep linking & it opens app from url. However it opens only the main url i.e. m.example.com & not m.example.com\products\iphone-5s\.
I'm using WebView to load my mobile website into an Android app.
Here's a code of xml file:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="m.example.com"
android:pathPrefix="/"
android:scheme="http" />
</intent-filer>
Java file:
private String url='m.example.com';
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
String action = intent.getAction();
Uri data = intent.getData();
mainWebView.loadUrl(url);
}
Can anybody help to resolve this?
Get the URL link with this code, right after Uri data = intent.getData();
String link = intent.getDataString();
Then, set simple if/else
if (link == null) {
mWebView.loadUrl("yourMainURL");
} else {
mWebView.loadUrl(link);
}