1. 程式人生 > >在電腦上開啟手機當前瀏覽的網頁

在電腦上開啟手機當前瀏覽的網頁

ShareUrl(專案名)

實現的功能:在電腦上開啟手機當前瀏覽的網頁

  • 在手機瀏覽器中分享當前開啟網頁的url到ShareUrl中
  • 把url上傳到野狗·實時通訊引擎
  • chrome外掛檢測到url後在新標籤頁開啟url

ShareUrl.apk的程式碼

build.gradle

compile 'com.wilddog.client:wilddog-sync-android:2.3.7'

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android" 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:name=".ShareUrlApplication" 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.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="text/*" /> </intent-filter> </activity> </application> </manifest>

ShareUrlApplication.java

import android.app.Application;

import com.wilddog.wilddogcore.WilddogApp;
import com.wilddog.wilddogcore.WilddogOptions;

public class ShareUrlApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        WilddogOptions options = new WilddogOptions.Builder().setSyncUrl("https://<your-app>.wilddogio.com").build();
        WilddogApp.initializeApp(this, options);
    }
}

MainActivity

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.wilddog.client.SyncError;
import com.wilddog.client.SyncReference;
import com.wilddog.client.WilddogSync;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    @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);
            SyncReference ref = WilddogSync.getInstance().getReference("share_url");
            HashMap<String, Object> data = new HashMap<>();
            data.put("time", System.currentTimeMillis());
            data.put("url", text);
            ref.setValue(data, new SyncReference.CompletionListener() {
                @Override
                public void onComplete(SyncError syncError, SyncReference syncReference) {
                    finish();
                }
            });
        } else {
            finish();
        }
    }
}

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>