import
android.content.Context;
import
android.content.Intent;
import
android.content.pm.PackageManager;
import
android.net.Uri;
import
android.os.Bundle;
import
android.text.TextUtils;
import
android.view.View;
import
android.widget.Button;
import
android.widget.Toast;
import
androidx.appcompat.app.AppCompatActivity;
import
java.util.List;
public
class
MainActivity
extends
AppCompatActivity {
private
Button startBtn;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startBtn = findViewById(R.id.idBtnStartLiveStream);
startBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
validateMobileLiveIntent(MainActivity.
this
);
}
});
}
private
boolean
canResolveMobileLiveIntent(Context context) {
Intent intent =
new
Intent(
"com.google.android.youtube.intent.action.CREATE_LIVE_STREAM"
).setPackage(
"com.google.android.youtube"
);
PackageManager pm = context.getPackageManager();
List resolveInfo = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return
resolveInfo !=
null
&& !resolveInfo.isEmpty();
}
private
void
validateMobileLiveIntent(Context context) {
if
(canResolveMobileLiveIntent(context)) {
startMobileLive(MainActivity.
this
);
}
else
{
Toast.makeText(context,
"Please Update your Youtube app."
, Toast.LENGTH_SHORT).show();
}
}
private
Intent createMobileLiveIntent(Context context, String description) {
Intent intent =
new
Intent(
"com.google.android.youtube.intent.action.CREATE_LIVE_STREAM"
).setPackage(
"com.google.android.youtube"
);
Uri referrer =
new
Uri.Builder()
.scheme(
"android-app"
)
.appendPath(context.getPackageName())
.build();
intent.putExtra(Intent.EXTRA_REFERRER, referrer);
if
(!TextUtils.isEmpty(description)) {
intent.putExtra(Intent.EXTRA_SUBJECT, description);
}
return
intent;
}
private
void
startMobileLive(Context context) {
Intent mobileLiveIntent = createMobileLiveIntent(context,
"Streaming via ..."
);
startActivity(mobileLiveIntent);
}
}