在电脑上打开手机当前浏览的网页

版权声明:***本文为博主原创文章,未经博主允许不得转载。*** https://blog.csdn.net/rrrrrr123rrr/article/details/84331083

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>

猜你喜欢

转载自blog.csdn.net/rrrrrr123rrr/article/details/84331083
今日推荐