I'm trying to deal with the new Android Lollipop MediaProjection API.
I have found that (at least on my stock Samsung Galaxy S4 jfltexx) when I start the intent to get permission to capture the screen (ProjectionManager.createScreenCaptureIntent()), I won't have a result in onActivityResult unless I have checked "Don't ask again" on the preceding try ...
private static final int ALLOW_SCREENSHOT_REQ = 102;
{
...
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
System.out.println("request permission");
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), ALLOW_SCREENSHOT_REQ);
}
...
}
And the result handling:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.println("RebootActivity.onActivityResult(" + requestCode + "," + resultCode + ", data)");
}
The permission dialog is showing well, but my activity gets hidden and it never goes to onActivityResult.
Any idea of what's going wrong?
Have you tried like this: See Full Example android-ScreenCapture
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_MEDIA_PROJECTION) {
if (resultCode != Activity.RESULT_OK) {
Log.i(TAG, "User cancelled");
Toast.makeText(getActivity(), R.string.user_cancelled, Toast.LENGTH_SHORT).show();
return;
}
Activity activity = getActivity();
if (activity == null) {
return;
}
Log.i(TAG, "Starting screen capture");
mResultCode = resultCode;
mResultData = data;
setUpMediaProjection();
setUpVirtualDisplay();
}
}
Hope it will helps you.
Related
I have an activity where I ask to enable Bluetooth and discovery mode.
Requests are executed correctly and are taken by onactivityresult().
The problem is in the discovery request.
If I click on reject then RESULT_CANCELED is taken correctly, while if I click on allow, the result code is 120 and therefore it is not RESULT_OK and I cannot start the activity, it is normal that it is 120 and not RESULT_OK?
MyActivity
public class TransitionActivity extends AppCompatActivity {
private static final int ENABLE_REQUEST = 0;
private static final int DISCOVER_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition);
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(bluetoothAdapter == null){ //bluetooth not supported
Toast.makeText(this, "Bluetooth not supported.", Toast.LENGTH_SHORT).show();
finish();
}
if(!bluetoothAdapter.isEnabled()){
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(i,ENABLE_REQUEST);
}
if(!bluetoothAdapter.isDiscovering()){
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(i,DISCOVER_REQUEST);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == ENABLE_REQUEST){
if(resultCode == RESULT_OK){
}
if(resultCode == RESULT_CANCELED){
Toast.makeText(this, "You need to enable the bluetooth.", Toast.LENGTH_SHORT).show();
finish();
}
}
if(requestCode == DISCOVER_REQUEST){
System.out.println("RESULT CODE" + resultCode); //it is 120
if(resultCode == RESULT_OK){ //skipped
Intent secondActivity = new Intent(this, com.project.secondActivity.class);
this.startActivity(secondActivity);
}
if(resultCode == RESULT_CANCELED){
finish();
}
}
}
}
Value of resultCode is same as discoverable duration EXTRA_DISCOVERABLE_DURATION passed in Intent.
and by default duration is 120. So it is OK.
If you will start activity like this
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 240);
startActivityForResult(i,DISCOVER_REQUEST);
It will return 240 as result code.
I always get ResultCode = 0 with code below.
This is my code for proccess setResult
#Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
Debug.i("Response Status Complete " + response.code());
if (response.code() == 200) {
Debug.i("Response Status Complete " + response);
mSectionTopic.isComplete = 1;
mSectionTopic.status = "completed";
Intent intent = new Intent();
intent.putExtra(Util.getIntentName("sectionTopic"), mSectionTopic);
intent.putExtra(Util.getIntentName("challengeId"), "AIzaSyBkSGK9geQ4QbnvXcQQ_m2JScd7r89xnJs");
intent.putExtra(Util.getIntentName("position"), mPosition);
setResult(Activity.RESULT_OK, intent);
finish();
} else {
showError(getString(R.string.text_post_question_failed));
}
But if code onBackPressed(); resultcode works correctly.
What's problem with my code?
--
This is my onActivityResult()
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE && resultCode == Activity.RESULT_OK) {
if (data == null) return;
int position = data.getIntExtra(Util.getIntentName("position"), 0);
mAdapter.setComplete(position);
mAdapter.notifyDataSetChanged();
}
}
You need to implement startActivityForResult() and onActivityResult() properly. Follow the instructions from here - Transfer data between two activities
So, I've created an share button on my application for twitter and facebook, and I need to know if it was shared or canceled.
On twitter I get the correct resultCode, but on facebook I don't. Is there a way to get the correct result code from facebook without using the facebookSDK?
Here's my default code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
Log.d(LOG, "OK");
Log.d(LOG, "CODE = " + resultCode);
//do something on google analytics
}
if (resultCode == Activity.RESULT_CANCELED) {
Log.d(LOG, "NOT OK");
Log.d(LOG, "CODE = " + resultCode);
Log.d(LOG, data == null ? "null" : data.getDataString());
//do something on google analytics
}
}
}
thanks
btw its my first question on stackoverflow.
Try this:
Start your intent like this:
startActivityForResult(intent, 1); //instead of startActivity(intent)
And retrieve the requestCode and resultCode by overriding onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
//intent 1 = successful (Facebook)
} else{
failed or canceled
}
}
}
This code working properly on lollypop version, But, when i use it on Kitkat, it always returns 0 when i choose "Yes/No" option from dialog.
btnSMSRestore.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (currentapiVersion >= android.os.Build.VERSION_CODES.KITKAT){
defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(SMSActivity.this);
if (!getPackageName().equals(defaultSmsApp))
{
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getPackageName());
//startActivity(intent);
startActivityForResult(intent, DEF_SMS_REQ);
}
}
else
{
new RestoreSMS().execute();
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch (requestCode)
{
case DEF_SMS_REQ:
if(resultCode == Activity.RESULT_OK)
{
new RestoreSMS().execute();
}
}
}
Activity.RESULT_OK value is -1 and resultCode in onActivityResult is always 0 when use with kitkat.
From the Android documentation:
onActivityResult Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it
returned, and any additional data from it. The resultCode will be
RESULT_CANCELED if the activity explicitly returned that, didn't
return any result, or crashed during its operation.
RESULT_CANCELED value is 0, probably on KitKat is not set a result value and the default one is returned.
As workaround on KitKat, you can try to check if your app is the default one when onActivityResult is fired. Try this code:
btnSMSRestore.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
if (!isDefaultSmsApp(SMSActivity.this)) {
{
Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getPackageName());
startActivityForResult(intent, DEF_SMS_REQ);
}
} else {
new RestoreSMS().execute();
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case DEF_SMS_REQ:
if (Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.KITKAT && isDefaultSmsApp(this) ||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && resultCode == Activity.RESULT_OK) {
new RestoreSMS().execute();
}
break;
}
}
#TargetApi(Build.VERSION_CODES.KITKAT)
public static boolean isDefaultSmsApp(Context context) {
return context.getPackageName().equals(Telephony.Sms.getDefaultSmsPackage(context));
}
I'm trying to load an image from gallery.
This is the onCreate() :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery_picture);
mThis = this;
mImageCache = new TakingPictureActivityCacheMngr(this);
initUi();
if (savedInstanceState == null) {
openGallery();
}
}
Here is the onSaveInstanceState():
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// save file url in bundle as it will be null on scren orientation
// changes
outState.putParcelable("file_uri", mFileUri);
if (com.isee.spot.toolkit.Config.IS_DEBUG) {
Log.d(TAG, "Instance was saved.");
}
}
On restore : (never called)
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// get the file Uri
mFileUri = savedInstanceState.getParcelable("file_uri");
}
The onCreate() is never called after opening the gallery, onDestroy() is called right when the gallery is opened and so it the onSaveInstanceState().
Here is the activity declaration in the manifest :
<activity
android:name=".GalleryPictureActivity"
android:configChanges="orientation|keyboard|keyboardHidden"
android:screenOrientation="portrait" >
</activity>
I overloaded the onActivityResult() :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if the result is loading image from gallery
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
...
Here is the answer,
The activity itself was correct.
The problem is with the activity which start it, I used : galleryPic.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Apparently this cause the onActivityResult(int, int, Intent) not be called.
Leaving this just for the chance of someone getting same behavior.
Thank you for trying to help, I guess you had no chance.
I may not be understanding the context of this code properly, but it looks like this is a custom image picking class ? You may be aware - but perhaps not - that there are built in SDK methods for this... see below:
private void pickImage() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, REQUEST_PICK_IMAGE);
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
switch (requestCode) {
case REQUEST_PICK_IMAGE:
if (resultCode == Activity.RESULT_OK) {
Log.v(TAG, "User Picked An Image");
handleImage(data.getData());
}
else if (resultCode == Activity.RESULT_CANCELED) {
Log.v(TAG, "User Cancelled Pick Image");
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}
}
private void handleImage(final Uri selectedImage) {
String filePath = selectedImage.getEncodedPath();
Log.v(TAG, " -- picked image is " + filePath);
MyImageView.setImage(selectedImage);
}