MediaPLayer not stopping - audio

I dont understand how to actually get this to work. My code is;
package com.example.user.medialert;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import java.io.IOException;
/**
* Created by User' on 9/24/2016.
*/
public class alert extends Activity {
final MediaPlayer Siren = new MediaPlayer() ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert);
MediaPlayer Siren = MediaPlayer.create(this, R.raw.alertsiren);
try {
Siren.prepare();
} catch (IOException e) {
e.printStackTrace();
}
Siren.start();
}
public void onClickOk1(View view) {
Siren.stop();
Intent intent = new Intent(alert.this, counter.class);
startActivity(intent);
}
}
The onClick changes the activity as it is supposed to however when I add the Siren.Stop(); code, 'Siren' is invalid, the error being cannot resolve symbol.
I have tried
MediaPlayer Siren = new MediaPlayer();
above the onCreate method, allowing siren to be used in the onClick, but it still doesn't stop playing the sound.
Any ideas?
EDIT
Added to show the code I have updated attempting to make final, and this now crashes the app.
Highlighting Siren under the onCreate method shows that the program isn't linking the Siren in the Siren.stop(); call

Related

Android studio: How to create BiometricManager and other bugs

So as a homework from collage i have to make a simple login app using fingerprint, i followed this video: How to Make a FingerPrint Authentication System in Android Studio and Java
But im getting some error that aren't explained and i can't find a answer form, this is my code:
package com.example.actividad14;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.graphics.Color;
import android.hardware.biometrics.BiometricManager;
import android.hardware.biometrics.BiometricPrompt;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.concurrent.Executor;
public class MainActivity extends AppCompatActivity {
#RequiresApi(api = Build.VERSION_CODES.Q)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView msg_txt = findViewById(R.id.txt_msg);
Button login_btn = findViewById(R.id.login_btn);
BiometricManager biometricManager = BiometricManager.from(this);
switch (biometricManager.canAuthenticate()){
case BiometricManager.BIOMETRIC_SUCCESS:
msg_txt.setText("You can use the fingerprint sensor to login");
msg_txt.setTextColor(Color.parseColor("#Fafafa"));
break;
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
msg_txt.setText("The device doesn't have a fingerprint sensor");
login_btn.setVisibility(View.GONE);
break;
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
msg_txt.setText("The biometric sensor is currently unavailable");
login_btn.setVisibility(View.GONE);
break;
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
msg_txt.setText("Your device doesn't have any fingerprint saved, please check your security settings");
login_btn.setVisibility(View.GONE);
break;
}
Executor executor = ContextCompat.getMainExecutor(this);
BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this, executor, new androidx.biometric.BiometricPrompt.AuthenticationCallback() {
#Override
public void onAuthenticationError(int errorCode, #NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
}
#Override
public void onAuthenticationSucceeded(#NonNull androidx.biometric.BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(getApplicationContext(),"Login Succes!", Toast.LENGTH_SHORT).show();
}
#Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
}
});
final BiometricPrompt.PrompInfo prompInfo = BiometricPrompt.PrompInfo.Builder()
.setTitle("Login")
.setDescription("Use your fingerprint to login in your app")
.setNegativeButtonText("Cancel")
.build();
login_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
biometricPrompt.authenticate(prompInfo);
}
});
}
}
At BiometricManager biometricManager = BiometricManager.from(this); im getting "cannot resolve method 'from' in 'biometricmanager'", but every video i have seen says that you create your BiometricManager this way.
At BiometricPrompt biometricPrompt = new BiometricPrompt(MainActivity.this, executor, new androidx.biometric.BiometricPrompt.AuthenticationCallback() { im getting "'BiometricPrompt' has private access in 'android.hardware.biometrics.BiometricPrompt'".
At final BiometricPrompt.PrompInfo prompInfo = BiometricPrompt.PrompInfo.Builder() im getting "cannot resolve PromptInfo"
And finally at biometricPrompt.authenticate(prompInfo); im getting "cannot resolve method '(authenticate(BiometricPrompt.PrompInfo))'"
Sorry for the inconvinience, i fount out what was wrong, instead of using:
import android.hardware.biometrics.BiometricManager;
import android.hardware.biometrics.BiometricPrompt;
use
import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
in the build.gradle i used
implementation 'androidx.biometric:biometric:1.2.0-alpha04'
But this can depend on new updates, so try with the newer ones also:
https://mvnrepository.com/artifact/androidx.biometric/biometric?repo=google
And finally i had PrompInfo instead of PromptInfo
This other post really helped me:
Android BiometricPrompt: Cannot resolve symbol PromptInfo

Alarm notification is not working for API>=26 android emulators

I am trying to send a notification that appears under the top navigation bar as follows:
My codes work for android emulator with API < 26, but they do not work for android emulator with API >= 26. I found that it is because I need to add channel, so I did. However, although the codes compile well and go through all loops, and there is no error in logcat. Still, the notification does not appear on the screen.
Any help will be greatly appreciated. FYI, here is my FragmentAlarm.java:
package com.example.dailybible3;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import java.util.Calendar;
public class FragmentAlarm extends Fragment {
private View view;
private CheckBox box_sun, box_mon, box_tue, box_wed, box_thu, box_fri, box_sat;
private CheckBox order_history, order_bible;
private CheckBox ninety_days, one_year;
private Button btn_save;
public static FragmentAlarm newInstance() {
FragmentAlarm fragmentAlarm = new FragmentAlarm();
return fragmentAlarm;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_alarm, container, false);
/* start of set notification */
createNotificationChannel();
/* end of set notification */
//save button
btn_save = (Button) view.findViewById(R.id.save_button);
btn_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String message = "This is a notificaiton example";
NotificationCompat.Builder builder = new NotificationCompat.Builder(
getContext(), "notifyBibleVerse"
)
.setSmallIcon(R.drawable.ic_bible_english)
.setContentTitle("Today's Verse")
.setContentText("Genesis 1 - 3")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
/* start of setting notification */
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("message", message);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager notiifcationManager = (NotificationManager) getActivity().getSystemService(
Context.NOTIFICATION_SERVICE
);
notiifcationManager.notify(0, builder.build());
toastMessage("Alarm is set!");
}
});
return view;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "DailyBibleChannel";
String description = "Channel for Bible Verse Reminder";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("notifyBibleVerse", name, importance);
channel.setDescription(description);
}
}
private void toastMessage(String message){
Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
}
}
I am still not sure how to solve the above problem, but I found the working code from here: https://codinginflow.com/tutorials/android/alarmmanager. The major difference I see is that the working code has AlarmManager. Hope this help someone who is having troubles to set a notification.

android studio error: reached end of file while parsing

package com.example.vicky.videostory;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
Button btnPlay;
VideoView myvideo;
MediaController mediaController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlay=(Button) findViewById(R.id.btn_Play);
myvideo=(videoView) findviewById(R.id.videoPlayer);
mediaController = new MediaController( this) ;
btnPlay.setOnClickListner(new View.OnClickListener() {
#Override
public void onClick(View view) {
String videoPath = "android.resource//com.example.vicky.videostory/"+R.raw.technology;
Uri uri=Uri.parse(videoPath);
myvideo.setVideoURI(uri);
myvideo.setMediaController(mediaController);
mediaController.setAnchorView(myvideo);
myvideo.start();
}
}
);
}
error: reached end of file while parsing
I think its just errors in brackets. Because when I click on an error it points towards brackets and says unexpected error.
this is the error please check whats wrong
You have problem in your syntax correct it first and then Rebuild Project also remember that follow the basics properly before doing code.

Android Studio - Creating a Loading/Splash Screen for WebView

I have followed through this tutorial (I am completely new to Android Studio): Loading/Splash Screen Tutorial
and I am unsure why my code isn't working correctly.
I get the loading circle come up on the app I have created, but it does not go away when the app loads the webpage.
I have gone over my code a few times now and I cannot see any errors (obviously there are some somewhere!) Just seems as if it isn't recognising that my webpage has loaded within the app.
Here is my code from my MainActivity.java file:
import android.net.http.SslError;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
String ShowOrHideWebViewInitialUse = "show";
private WebView myWebView;
private ProgressBar spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webView);
spinner = (ProgressBar)findViewById(R.id.progressBar1);
myWebView.setWebViewClient(new CustomWebViewClient());
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setDatabaseEnabled(true);
myWebView.getSettings().setMinimumFontSize(1);
myWebView.getSettings().setMinimumLogicalFontSize(1);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.loadUrl("https://node-red-test.ftp21.net:1024/ui");
myWebView.setWebViewClient(new WebViewClient() { #Override public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){ handler.proceed(); } });
}
// This allows for a splash screen
// (and hide elements once the page loads)
private class CustomWebViewClient extends WebViewClient {
#Override
public void onPageStarted(WebView myWebView, String url, Bitmap favicon) {
// only make it invisible the FIRST time the app is run
if (ShowOrHideWebViewInitialUse.equals("show")) {
myWebView.setVisibility(myWebView.INVISIBLE);
}
}
#Override
public void onPageFinished(WebView view, String url) {
ShowOrHideWebViewInitialUse = "hide";
spinner.setVisibility(View.GONE);
view.setVisibility(myWebView.VISIBLE);
super.onPageFinished(view, url);
}
}
#Override
public void onBackPressed() {
if (myWebView.canGoBack()) {
myWebView.goBack();
} else {
super.onBackPressed();
}
}
}
I am currently running Android Studio version 2.1.2 - due to problems with later versions that I came across when following another tutorial.
Thanks,

Do I need to create these many classes for this android app?

I am an absolute beginner at programming so I just want to make sure I am doing this right or if there is an easier way. basically the app I am creating is a school app that will list all courses needed for different majors.
So opening screen has two buttons undergrad and grad. The next screen would list all majors for either grad or undergrad, which ever one the user chose, then say a person clicked accounting it would then list all courses for accounting major. My question is do I need to create a seperate class for each course and major?? Below is the code I have been doing.
package Class.Review;
import Class.Review.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
public class ClassReviewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Undergrad = (Button) findViewById(R.id.BUNGrad);
Undergrad.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent myIntent =
new Intent(view.getContext(), undergrad.class);
startActivityForResult(myIntent, 0);
}
});
}
}
Then say the user clicked undergrad i created another class
package Class.Review;
import Class.Review.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import android.content.Intent;
public class undergrad extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.undergrad);
setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent replyIntent = new Intent();
setResult(RESULT_OK, replyIntent);
finish();
}
});
}
private void setOnClickListener(OnClickListener onClickListener) {
// TODO Auto-generated method stub
Button Accounting = (Button) findViewById(R.id.Accounting);
Accounting.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent myIntent =
new Intent(view.getContext(), accounting.class);
startActivityForResult(myIntent, 0);
}
});
}
}
Then choice a specific course within Accounting
package Class.Review;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
public class accReview extends Activity{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accreview);
setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent replyIntent = new Intent();
setResult(RESULT_OK, replyIntent);
finish();
}
});
}
private void setOnClickListener(OnClickListener onClickListener) {
// TODO Auto-generated method stub
Button review = (Button) findViewById(R.id.reviews);
review.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
Intent myIntent =
new Intent(view.getContext(), fundReview.class);
startActivityForResult(myIntent, 0);
}
});
}
}
Unless you need different functionality for each course, you're probably better of using a single Course class with a few fields containing course name (String), max number of students (some integer type), maybe a Professor object, or whatever you need to describe the course. You don't want to rewrite, recompile and redistribute the code each time you want to add, remove or rename a course, do you?
Also, this doesn't just apply to this specific case, this question has probably been asked tons of times before.

Resources