下面是两个应用程序的源代码,一个隐式意图和另一个试验,前者发送隐式意图以打开网站,而后者则接收和显示网站。
我还在试验应用的清单下方给出了。试用版应用未收到此意图。我是一个初学者,不知如何进行。
package vogella.unni.com.implicitintents;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.ShareCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText et1, et2, et3;
private Button b1, b2, b3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText) findViewById(R.id.et1);
}
public void openWebsite(View view) {
String website = et1.getText().toString();
// Uri web = Uri.parse(website);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(website));
Intent chooser = Intent.createChooser(intent, "Please choose your browser");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
} else {
Log.d("ImplicitIntents", "I am sorry..cannot open the website");
}
}
}
package com.example.venug.trials;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.net.URI;
public class MainActivity extends AppCompatActivity {
TextView tv1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
Intent intent = getIntent();
Uri uri = intent.getData();
if (uri != null){
String uri_string = uri.toString();
tv1.setText(uri_string);
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.venug.trials">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android:permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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="cfm-india.com"/>
</intent-filter>
</activity>
</application>
</manifest>