In this article, we will do three main things.
The code is here. SharedText
First, add an intent-filter to Manifest.xml.
AndroidManifest.xml
// ...Omission...
<activity android:name=".SharedActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
// ...Omission...
Next, try receiving it with SharedActivity.java.
SharedActivity.java
public class SharedActivity extends AppCompatActivity {
TextView textView;
ImageView thumbnail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared);
textView = findViewById(R.id.movieId);
thumbnail = findViewById(R.id.thumbnail);
Intent intent = getIntent();
String id = null;
if(intent.getType().equals("text/plain")){
String text = intent.getStringExtra(Intent.EXTRA_TEXT);
Log.d("text",text);
}
}
// ...Omission...
}
Now you can receive the link passed by Intent to text.
[reference]
This obediently extracted the ID from the link string.
https://youtu.be/yT_ylSCgY6Q
Since you can get the character string like this, the trailing yT_ylSCgY6Q
is the ID of the video.
By the way, the link above will take you to BUMP OF CHICKEN feat. HATSUNE MIKU "ray".
To get YouTube video information, like the article Getting YouTube video information using Data API v3, ** Youtube Data API ** is used. Obtain the API key and enter the following URL in your browser. For information on how to get the API Key, please refer to Getting YouTube video information using Data API v3.
https://www.googleapis.com/youtube/v3/videos?id=yT_ylSCgY6Q&key=【API Key】&fields=items(id,snippet(channelTitle,title,thumbnails),statistics)&part=snippet,contentDetails,statistics
Enter the acquired key in the [API Key] part.
The result is as follows.
result
{
"items": [
{
"id": "yT_ylSCgY6Q",
"snippet": {
"title": "BUMP OF CHICKEN feat. HATSUNE MIKU「ray」",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/yT_ylSCgY6Q/default.jpg ",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/yT_ylSCgY6Q/mqdefault.jpg ",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/yT_ylSCgY6Q/hqdefault.jpg ",
"width": 480,
"height": 360
},
"standard": {
"url": "https://i.ytimg.com/vi/yT_ylSCgY6Q/sddefault.jpg ",
"width": 640,
"height": 480
},
"maxres": {
"url": "https://i.ytimg.com/vi/yT_ylSCgY6Q/maxresdefault.jpg ",
"width": 1280,
"height": 720
}
},
"channelTitle": "BUMP OF CHICKEN"
},
"statistics": {
"viewCount": "12691874",
"likeCount": "65136",
"dislikeCount": "3062",
"favoriteCount": "0",
"commentCount": "4337"
}
}
]
}
[reference]
dependencies {
// ...Omission...
implementation 'com.google.code.gson:gson:2.8.4'
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
// ...Omission...
}
[reference]
ApiService.java
public interface ApiService {
@Headers("Accept-Language: ja")
@GET("/youtube/v3/videos?")
Call<APIResponse> requestMovie(@Query("id") String id,
@Query("key") String key,
@Query("fields") String fields,
@Query("part") String part);
}
APIResponse.java
public class APIResponse {
public List<Item> items;
public List<Item> getItems() {
return items;
}
}
Item.java
public class Item {
@SerializedName("id")
private String id;
@SerializedName("snippet")
private Snippet snippet;
@SerializedName("statistics")
private Statistics statistics;
public String getId() {
return id;
}
public Snippet getSnippet() {
return snippet;
}
public Statistics getStatistics() {
return statistics;
}
}
Snippet.java
public class Snippet {
@SerializedName("title")
private String title;
@SerializedName("thumbnails")
private Thumbnails thumbnails;
public class Thumbnails {
@SerializedName("default")
public Default aDefault;
@SerializedName("medium")
public Medium medium;
@SerializedName("high")
public High high;
public High getHigh() {
return high;
}
@SerializedName("standard")
public Standard standard;
@SerializedName("maxres")
public Maxres maxres;
public class Default {
String url;
String width;
String height;
}
public class Medium {
String url;
String width;
String height;
}
public class High {
String url;
String width;
String height;
public String getUrl() {
return url;
}
}
public class Standard {
String url;
String width;
String height;
}
public class Maxres {
String url;
String width;
String height;
}
}
@SerializedName("channelTitle")
String channelTitle;
public String getTitle() {
return title;
}
public Thumbnails getThumbnails() {
return thumbnails;
}
public String getChannelTitle() {
return channelTitle;
}
}
Statistics.java
public class Statistics {
@SerializedName("viewCount")
String viewCount;
@SerializedName("likeCount")
String likeCount;
@SerializedName("dislikeCount")
String dislikeCount;
@SerializedName("favoriteCount")
String favoriteCount;
@SerializedName("commentCount")
String commentCount;
public String getViewCount() {
return viewCount;
}
public String getLikeCount() {
return likeCount;
}
public String getDislikeCount() {
return dislikeCount;
}
public String getFavoriteCount() {
return favoriteCount;
}
public String getCommentCount() {
return commentCount;
}
}
As I was writing the article, I noticed that I wrote it without thinking too much about modifiers, so I would be grateful if you could do something about it.
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Retrofit mRetrofit;
private ApiService mService;
//API KEY Created from Console
private final String API_KEY = "dummyAPIKey"; //Please change the API Key as appropriate.
private final String FIELDS = "items(id,snippet(channelTitle,title,thumbnails),statistics)";
private final String PART = "snippet,contentDetails,statistics";
// "BUMP OF CHICKEN feat. HATSUNE MIKU「ray」"
private String testId = "yT_ylSCgY6Q";
ImageView picture;
TextView title;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRetrofit = new Retrofit.Builder()
.baseUrl("https://www.googleapis.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
mService = mRetrofit.create(ApiService.class);
picture = findViewById(R.id.picture);
title = findViewById(R.id.title);
button = findViewById(R.id.button);
button.setOnClickListener(onClickListener);
}
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
Call<APIResponse> mCall = mService.requestMovie(testId,API_KEY,FIELDS,PART);
mCall.enqueue(new Callback<APIResponse>() {
@Override
public void onResponse(Call<APIResponse> call, Response<APIResponse> response) {
//Get Item from response
List<Item> items = response.body().getItems();
for(Item item : items){
Uri uri = Uri.parse(item.getSnippet().getThumbnails().getHigh().getUrl());
Uri.Builder builder = uri.buildUpon();
AsyncTaskHttpRequest task = new AsyncTaskHttpRequest(picture);
task.execute(builder);
title.setText(item.getSnippet().getTitle());
}
}
@Override
public void onFailure(Call<APIResponse> call, Throwable t) {
}
});
}
};
}
In addition, I referred to the following site to get the image from the URL and display it in ImageView. Note that even if you copy and paste the above code, the AsyncTaskHttpRequest class will be undefined. [reference]
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">
<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title"
app:layout_constraintVertical_bias="0.3" />
</android.support.constraint.ConstraintLayout>
activity.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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".SharedActivity">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.201"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/movieId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
AndroidManifest.xml Just in case, I will also post the manifest file.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tomiyama.noir.sharedtext">
<uses-permission android:name="android.permission.INTERNET" />
<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>
</activity>
<activity android:name=".SharedActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
</application>
</manifest>
[reference] It was very helpful when accessing the API around Retrofit. Also, for details, refer to the code posted on Github.
Recommended Posts