ShareUrl(项目名)
实现的功能:在电脑上打开手机当前浏览的网页
- 在手机浏览器中分享当前打开网页的url到ShareUrl中
- 把url上传到gitee的代码片段中
- chrome插件检测到url后在新标签页打开url
ShareUrl.apk的代码
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.ylw.shareurl">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity
package com.ylw.shareurl;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
if (intent.getExtras() != null) {
TextView subjectView = findViewById(R.id.subject);
TextView textView = findViewById(R.id.text);
CharSequence subject = intent.getExtras().getString(Intent.EXTRA_SUBJECT);
CharSequence text = intent.getExtras().getString(Intent.EXTRA_TEXT);
subjectView.setText(subject);
textView.setText(text);
try {
JSONObject file = new JSONObject();
file.put("time", System.currentTimeMillis());
file.put("url", text);
JSONObject jsonObject = new JSONObject("{\n" +
" \"access_token\": \"96a4681a09abb60000000000000000\",\n" +
" \"files\": { \"ShareUrl\": { \"content\": \"" + file.toString().replaceAll("\"", "\\\\\"") + "\" } }, \"description\": \"ShareUrl\"\n" +
" }");
new Thread(() -> {
HttpURLConnection conn = null;
OutputStream os = null;
InputStream is = null;
try {
URL url = new URL("https://gitee.com/api/v5/gists/z0s3edyri6oxxxxxxxxxxxx");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PATCH");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.connect();
os = new DataOutputStream(conn.getOutputStream());
os.write(jsonObject.toString().getBytes());
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
is = conn.getInputStream();
ByteArrayOutputStream bos;
bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int len;
while ((len = is.read(b)) != -1) { //先读到内存
bos.write(b, 0, len);
}
Log.i(TAG, "onCreate: " + new String(bos.toByteArray()));
showToast("SUCCESS");
new Handler(Looper.getMainLooper()).post(this::finish);
} else {
showToast("responseCode:" + responseCode);
}
} catch (Exception e) {
showToast(e.getMessage());
e.printStackTrace();
} finally {
close(is);
close(os);
if (conn != null) {
conn.disconnect();
}
}
}).start();
} catch (JSONException e) {
e.printStackTrace();
}
} else {
finish();
}
}
void close(Closeable closeable) {
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void showToast(String msg) {
new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(this, msg, Toast.LENGTH_LONG).show());
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ProgressBar
android:id="@+id/progressBar"
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:padding="20dp"
android:text="subject"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/progressBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:padding="20dp"
android:text="text"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/subject" />
</android.support.constraint.ConstraintLayout>