Androidでintentserviceからインテントを共有する方法は?
例に入る前に、Androidのインテントサービスとは何かを知っておく必要があります。インテントサービスは、バックグラウンド操作を非同期的に実行します。ユーザーがアクティビティからstartService()を呼び出す場合、リクエストごとにインスタンスは作成されません。サービスクラスで何らかのアクションを実行した後、サービスを停止します。そうでない場合は、stopSelf()を使用してサービスを停止する必要があります。
この例は、intentserviceからインテントを共有する方法について示しています。
ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。
ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。
<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android = "https://schemas.android.com/apk/res/android"
xmlns:app = "https://schemas.android.com/apk/res-auto"
xmlns:tools = "https://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
tools:context = ".MainActivity">
<TextView
android:id = "@+id/text"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "Start Service"
android:textSize = "25sp"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintLeft_toLeftOf = "parent"
app:layout_constraintRight_toRightOf = "parent"
app:layout_constraintTop_toTopOf = "parent" />
</android.support.constraint.ConstraintLayout> 上記のコードでは、テキストビューを使用しています。ユーザーがテキストビューをクリックすると、AndroidOSからデフォルトの共有ダイアログが開きます。
ステップ3-次のコードをsrc/MainActivity.java
に追加しますpackage com.example.andy.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.text);
text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(MainActivity.this, service.class));
}
});
}
} service.classファイルというクラスを作成し、次のコードを追加します–
package com.example.andy.myapplication;
import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
public class service extends IntentService {
public static volatile boolean shouldStop = false;
public service() {
super(service.class.getSimpleName());
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
protected void onHandleIntent(Intent intent) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Tutorialspoint.com");
startActivity(Intent.createChooser(sharingIntent, "Sharing"));
if(shouldStop) {
stopSelf();
return;
}
}
} サービスを停止するには、サービスクラスで次のコードを使用します–
stopSelf();
ステップ4 −次のコードをmanifest.xmlに追加します
<?xml version = "1.0" encoding = "utf-8"?> <manifest xmlns:android = "https://schemas.android.com/apk/res/android" package = "com.example.andy.myapplication"> <uses-permission android:name = "android.permission.WAKE_LOCK"/> <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> <service android:name = ".service"/> </application> </manifest>
アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します–
上記の結果では、アプリケーションのデフォルト画面が表示されています。ユーザーがテキストビューをクリックすると、以下に示すように、モバイルOSからのデフォルトの共有ダイアログが表示されます–
-
Android から PC にファイルを転送する方法
Android から PC にファイルを転送: 現在、私たちは PC よりも携帯電話を頻繁に使用しています。したがって、ほとんどのファイルが一般的に PC ではなくスマートフォンに存在するのは当然のことです。ここでの唯一の問題は、Android または iPhone には、ユーザーが超えることができないメモリ制限があることです。したがって、すべてのデータを携帯電話よりも空き容量のある PC に保存することは理にかなっています。 しかし、既存のファイルを Android から PC に転送するのも大変な作業です。すべてのファイルとフォルダーを携帯電話から PC に手動で転送することを計画して
-
Android でスクリーン ミラーリングを PC に共有する方法
結婚式の写真やお気に入りの映画を Android フォンに保存して、大画面で見たいですか?どのように進めますか?どうしますか? さて、解決策は Android から PC へのスクリーン ミラーリングです。はい、画面を共有することで、お気に入りの映画を友達や家族と見ることができます。この投稿では、Android で画面ミラーリングを行う方法について説明します。 ステップ 1:USB デバッグを開始する まず、USBデバッグとは何かを理解してください。このプロセスでは、開発者モードが Android フォンにあり、PC などのテスト デバイスに USB 経由ですべてのアプリに直接アクセスでき