How to play Media player after backing from another activity? - android

The Media Player work well when I start the app and click the button. However, when I hit the Home button or when I go to another activity and then I cannot play Media file any more when I come back to the MainActivity. Here is my code:
public void playMedia(View view){
if(mp.isPlaying()){
mp.pause();
}
else
mp.start();
}
#Override
protected void onPause(){
super.onPause();
SOSPlayer.pause();
}
#Override
protected void onResume(){
super.onResume();
SOSPlayer.seekTo(0);
}
#Override
protected void onDestroy() {
super.onDestroy();
SOSPlayer.stop();
SOSPlayer.release();
}
#Override
protected void onStop() {
super.onStop();
SOSPlayer.stop();
}

I thing you call playMedia method on layout as:
<Button
...
android:onClick="playMedia"
..
/>
What you need is to play that media again after getting back from previous activity .. you need to start that activity for result with startActivityForResult and listen to feedback onActivityResult to play the media again. Here is a simple implementation:
private static final int REQUEST_ID = 0x0001;
private void startActivity() {
Intent intent = new Intent(getApplicationContext(), DifferentActivity.class);
startActivityForResult(intent, REQUEST_ID);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ID:
playMedia(null);
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}

Related

Zxing scanned result how to paste on editText

Hello I want to thank you in advance for your answers. My problem is I am using Zxing to scan qr codes. And I want to paste the scan value on the my edit text at Login Activity.
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.google.zxing.Result;
import com.logizard.logizard_go.R;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class ScanResult extends AppCompatActivity implements ZXingScannerView.ResultHandler{
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_result);
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
setContentView(mScannerView); // Set the scanner view as the content view
}
#Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
#Override
public void handleResult(Result rawResult) {
// Do something with the result here
// Log.v("tag", rawResult.getText()); // Prints scan results
// Log.v("tag", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)
Login.editTextUser.setText(rawResult.getText());
onBackPressed();
// If you would like to resume scanning, call this method below:
//mScannerView.resumeCameraPreview(this);
}
}
This is the sample code that I use but every time I use the button for scan nothing happens.
It looks like that you have a Login(Activity) that open the ScanResult(Activity) and you want to return the value of the scanned to Login
I suggest to you to look this doc, but also maybe you could move the handlingscan part of your code to Login activity
... implements ZXingScannerView.ResultHandler{
#Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
}
#Override
public void onPause() {
super.onPause();
mScannerView.stopCamera(); // Stop camera on pause
}
#Override
public void handleResult(Result rawResult) {
editTextUser.setText(rawResult.getText());
}
}
BETTER do like that
in Login
static final int GET_SCANNED = 1;
...
private void openScanResult(){
Intent i = new Intent(this, ScanResult.class);
startActivityForResult(i, GET_SCANNED);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_SCANNED) {
if(resultCode == Activity.RESULT_OK){
String result = data.getStringExtra("scanned");
editTextUser.setText(result)
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
in ScanResult
#Override
public void handleResult(Result rawResult) {
Intent returnIntent = new Intent();
returnIntent.putExtra("scanned",rawResult.getText());
setResult(Activity.RESULT_OK,returnIntent);
finish();
}

Zxing - pressing back button from camera view, before reading any qr codes

This is part of the code that I've used to implement the QR code scanner,using zxing library.
Once the button is clicked, mScannerView.stopCamera() is activated, and the screen for scanning is shown. If I press phones back button from that screen, before any qrcode is read, the app completely closes and it does not go back to the previous activity. How do I go back to the previous activity(from where the startCamera() was called) when I press the back button on the phone, before reading any qr codes? Any ideas?
New Activity:
import com.google.zxing.Result;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class New extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
}
public void onClick(View v){
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
#Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
#Override
public void handleResult(Result result) {
//Do anything with result here :D
Log.w("handleResult",result.getText( ));
AlertDialog.Builder builder= new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
//Resume scanning uncomment below
//mScannerView.resumeCameraPreview(this);
}
}
Personally, I use this package. See https://github.com/journeyapps/zxing-android-embedded. Clear instruction is available for setting it up in Gradle.
In your original activity, add the followings.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null && resultCode == RESULT_OK) {
// if user scanned and the result is valid, do your stuff here
} else {
// if user pressed back or there's error, do your stuff here
}
}

Android call method from intent

I've this code:
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, ConnectDialog.class);
update();
}
private void update(){
if(a)
startActivity(intent);
else{
//code
}
}
}
And this:
public class ConnectDialog extends Activity{
private Button btn;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect_dialog);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
finish();
}
});
}
}
The problem is: when I click on the button of the Intent, is it possible to execute the method update of the main activity again? Thank you very much
Just put the call to update into onResume() of the MainActivity. This way, it will be called at first startup and when the MainActivity is shown again later on:
#Override
protected void onResume(){
super.onResume;
update();
}
I would use the onActivityResult method.
Change your update method to
int YOUR_RESULT = 100;
private void update(){
if(a)
startActivityForResult(intent, YOUR_RESULT);
else{
//code
}
}
then in that same activity, use this method:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == YOUR_RESULT) {
update();
}
}
If you use this method, the update() method will only get called when coming back from that activity.
if u check the Android Life Cycle u will get the answer why the Update is not executing..
How ever to call the update method on click..
private void update(){
if(a)
startActivityForResult(intent, 0);
else{
//code
}
}
and use onActivityResult to call the update method again
#Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);
update();
}

Facebook ShareDialog: back button doesn't catch by Activity

I follow tutorial for write ShareDialog of facebook api, but it doesn't work. When activity is launched, after setting up environment, i show automatically ShareDialog. If i press back button when that dialog is showed, my app close dialog, and open it again. It seems that back button isn't catch by activity. This is my code:
public class ShareScoreOnFb extends FragmentActivity {
private static UiLifecycleHelper uiHelper;
private static Activity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_score_on_fb);
activity = this;
uiHelper = new UiLifecycleHelper(this, null);
uiHelper.onCreate(savedInstanceState);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data,
new FacebookDialog.Callback() {
#Override
public void onComplete(PendingCall pendingCall, Bundle data) {
String gesture = FacebookDialog
.getNativeDialogCompletionGesture(data);
if (gesture != null) {
if ("post".equals(gesture)) {
Toast.makeText(ShareScoreOnFb.this,
"success",
Toast.LENGTH_SHORT).show();
}
ShareScoreOnFb.this.finish();
}
ShareScoreOnFb.this.finish();
}
#Override
public void onError(PendingCall pendingCall,
Exception error, Bundle data) {
Toast.makeText(ShareScoreOnFb.this, error.toString(),
Toast.LENGTH_SHORT).show();
ShareScoreOnFb.this.finish();
}
});
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
#Override
protected void onResume() {
super.onResume();
uiHelper.onResume();
share();
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
#Override
public void onBackPressed() {
Log.i("TAG", "backpressed");
super.onBackPressed();
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
private void share() {
Intent intent = this.getIntent();
int myScore = intent.getIntExtra("MYSCORE", -1);
int oppScore = intent.getIntExtra("OPPONENT_SCORE", -1);
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
.setName("Score").setLink("http://www.google.it")
.setDescription(myScore + " " + oppScore)
.setPicture("http://www.cs.cmu.edu/~junggon/tools/gear.png")
.build();
uiHelper.trackPendingDialogCall(shareDialog.present());
}
}
I can't find any solution, and i'm going to be crazy!
When you call the native share dialog (in FacebookDialog), it will start another activity (call it Activity Foo) in the Facebook app. So if you click on the back button, it's Activity Foo that catches the back button, and finishes Activity Foo. I believe here's the sequence of events:
Your ShareScoreOnFb activity calls shareDialog.present()
Activity Foo starts, and shows the share dialog
You click back
Activity Foo finishes, and calls your callback (in onActivityResult)
once onActivityResult completes, your ShareScoreOnFb activity's onResume is called, and it will try to share() again
This will handle result without back press.
I have tried this and got success.
On Click (For Link Sample (You can modify your content)) :
shareDialog = new ShareDialog(MainActivity.this);
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("Hello Facebook")
.setContentDescription("The 'Hello Facebook' sample showcases simple Facebook integration")
.setContentUrl(Uri.parse("http://developers.facebook.com/android"))
.build();
shareDialog.show(linkContent, ShareDialog.Mode.AUTOMATIC);
In onActivityResult method :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}

Android : Close Activity after Result

I've written a Small Google Maps Application, which makes me a few Problems.
The LocationListener i wrote, overrides the onProviderDisabled Method to call an Activity, where the User can Switch on his GPS.
This do the Job well, but one Problem is still there. If i close the Maps_Activity and after that shut down the GPS, it seems, that the Listener is already running, because the Activity (the one who says me, that the GPS is not running) still pops up.
The LocationListner as followed:
public class LocationUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
mMyLocationOverlay.enableMyLocation();
mMyLocationOverlay.runOnFirstFix(new Runnable() {
#Override
public void run() {
mMapView.getController().animateTo(
mMyLocationOverlay.getMyLocation());
}
});
}
#Override
public void onProviderDisabled(String provider) {
Intent i = new Intent(MapActivity.this, NoGpsActivity.class);
startActivity(i);
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(getApplicationContext(), "GPS ready", Toast.LENGTH_LONG).show();
}
}
The NoGPSAcivity.class as followed:
public class NoGpsActivity extends Activity {
private OnClickListener mEnableGPSListener = new OnClickListener() {
#Override
public void onClick(View v) {
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gps_not_available_overlay);
Button mEnableGPS = (Button)findViewById(R.id.enablegps_button);
mEnableGPS.setOnClickListener(mEnableGPSListener);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 0){
finish();
}
}
}
Thank you for your help!
In your onDestroy() method you need to call removeUpdates(locationListener) so it will stop receiving updates.
Also another thing to watch out for is that the "Home" key in Android generally means "minimize" (leaving your program in the background) while the "Back" key means "close". So if your hitting home to exit, most likely it is still running.

Categories

Resources