Action when sound stops? Android Studio - android

I'm making my first app in Android Studio. I got this activity, where it just plays a sound with MediaPlayer, and then it is supposed to return to the main activity, either by a button (works fine) or when the sound stops(what i'm missing). Is there an easy way to check if the sound has stopped playing, and then let that trigger an action?
Here is my code so far:
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
public class image_button_2 extends ActionBarActivity implements View.OnClickListener {
ImageButton imageButton6;
MediaPlayer mySound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_button_2);
imageButton6 = (ImageButton) findViewById(R.id.imageButton6);
imageButton6.setOnClickListener(this);
mySound = MediaPlayer.create(this, R.raw.hb);
mySound.start();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_image_button_2, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void imageButton6Click(){
mySound.stop();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.imageButton6:
imageButton6Click();
break;
}
}
}

Try this out , This will take you into MainActivity when music is finished
mySound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
imageButton6Click();
}
});

I think you are looking for OnCompletionListener callback.

Related

How can i generate vibration according to a binary pattern using Android studio?

I'm a android self learner. I want to generate vibration for binary string.
Here is the code I used
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnVibrate=(Button)findViewById(R.id.btnsum);
// Set on click listener and start vibrate service when clicked on Button Vibrate
btnVibrate.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
// Create a New Intent and start the service
Intent intentVibrate =new Intent(getApplicationContext(),VIBRATOR_SERVICE.getClass());
startService(intentVibrate);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I want to generate vibration according to a binary string.If i give like '110' a particular vibration should generate for that string..
Thanks in advance.
You can split your string into an array
char[] charPattern = yourString.toCharArray();
Then convert your char array into a long array:
long[] pattern = new long[charPattern.length];
for (int i = 0; i < charPattern.length; i++) {
long[i] = char[i] = Long.valueOf(char[i])
}
And use your array in the vibrator.
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(pattern, 1);

How do I switch the if and if else when switching the text between buttons in android studio?

I am trying to switch the text between buttons like a puzzle game using the grid layout. I tried using an if statement but I feel I am doing it wrong. can any one give suggestions? Here is my java code
package com.example.kellito13.test;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn=(Button)findViewById(R.id.btn);
final Button btnOne=(Button)findViewById(R.id.btnOne);
final Button btnTwo=(Button)findViewById(R.id.btnTwo);
final Button btnFive=(Button)findViewById(R.id.btnFive);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(v == btnOne){
Button btn = (Button) v;
btn.setText("B");
btnOne.setText("A");
}
else if (v == btnFive){
btn.setText("E");
btnFive.setText("A");
}
/* Button btn = (Button) v;
btn.setText("B");
btnOne.setText("A");*/
}
});
btnOne.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Button btnOne = (Button) v;
btn.setText("A");
btnOne.setText("B");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Try this way
#Override
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.btnOne:
btn.setText("B");
btnOne.setText("A");
break;
case R.id.btnFive:
btn.setText("E");
btnFive.setText("A");
break;
default :
}
}
package com.example.kellito13.test;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity Implement View.OnclickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn=(Button)findViewById(R.id.btn);
final Button btnOne=(Button)findViewById(R.id.btnOne);
final Button btnTwo=(Button)findViewById(R.id.btnTwo);
final Button btnFive=(Button)findViewById(R.id.btnFive);
//In oncreate method register the listener for all button
btn.setOnClickListener(this);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
btnFive.setOnClickListener(this);
}
//the following will be your original code....
#Override
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.btn:
//do stuff
break;
case R.id.btnOne:
//do stuff
break;
case R.id.btnTwo:
//do stuff
break;
case R.id.btnFive:
//do stuff
break;
default :
//do stuff
}
}
}

Implementing GoogleAnalytics in Android App

I am implementing Google Analytics in Android App using eclipse. After lot of research i implemented it. Here is my Code:
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.google.analytics.tracking.android.EasyTracker;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.StandardExceptionParser;
public class MainActivity extends Activity {
private EasyTracker easyTracker = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
easyTracker = EasyTracker.getInstance(MainActivity.this);
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
easyTracker.send(MapBuilder.createEvent("your_action",
"envet_name", "button_name/id", null).build());
}
});
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
try {
int a[] = new int[2];
int num = a[4];
} catch (ArrayIndexOutOfBoundsException e) {
easyTracker.send(MapBuilder.createException(
new StandardExceptionParser(MainActivity.this, null)
.getDescription(Thread.currentThread().getName(), e), false).build());
}
}
});
}
#Override
public void onStart() {
super.onStart();
EasyTracker.getInstance(this).activityStart(this);
}
#Override
public void onStop() {
super.onStop();
EasyTracker.getInstance(this).activityStop(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The code compiled successfully but the problem which i am facing is that it is not sending any information in the GoogleAnalytics account.I have not received any part of information in google Analytics.

i am making a note app i want to make id´s in an loop and then i want to check the id with a onclicklistener

i am trying to make a note app.in an array i want to make id´s.
i want to check the id with an onclicklistener.
in the onclicklistener i make an Intent and an ExtraString
in this Extrastring i want to write the content from the note file i created
this is the code :
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
public class NotizOeffnungsMenue extends Activity implements View.OnClickListener {
Button[] NoteListBtn ;
String[] NoteList ;
int [] e;
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notiz_oeffnungs_menue);
LinearLayout l1 = (LinearLayout)findViewById(R.id.layout1);
NoteListBtn = new Button[fileList().length];
NoteList = fileList();
i = 0;
while (i < fileList().length)
{
NoteListBtn[i] = new Button(this);
NoteListBtn[i].setText(NoteList[i]);
NoteListBtn[i].setOnClickListener(this);
l1.addView(NoteListBtn[i]);
NoteListBtn[i].setId(i);
e [i] =i;
i++;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.notiz_oeffnungs_menue, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View view)
{
if(e[] == view.getId())
{
Intent switchintent = new Intent(this,NotizActivity.class);
String EXTRA_INHALT;
startActivity(switchintent);
}
}
}
In onClick() I would recommend u to use switch case statement
switch(v.getId()){
case R.id.butnid:
//do something here
break;
case R.id.butnid2:
//do something here
break;
}
Make as many cases as u want.
And remember that the buttons u use should do this
butnid.setOnClickListener(this);
butnid2.setOnClickListener(this);
//And so on for ever id u use in on click
You can use like this:
#Override
public void onClick(View view)
{
Intent switchintent = new Intent(this,NotizActivity.class);
switchintent.putExtra("EXTRA_INHALT", e[view.getId()]);
switchintent.putExtra("EXTRA_INHALT2", NoteList[view.getId()]);
startActivity(switchintent);
}
Instead of comparing id, you can use that as a position. Because you are already setting that position as Id.
Hope this helps.

Syntax error on token "}", { expected after this token android

I'm trying to make a timer for the android app I'm developing and am trying to make a test timer that will display the time left. I have tried a lot of websites but none work. Here is the complete code from the activity:
package com.hunter.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class Settings extends Activity {
//intent to return to menu from settings
public void back (View view) {
Intent back = new Intent(this, MainActivity.class);
startActivity(back);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
//calls on textview
final TextView text1 = (TextView)findViewById(R.id.textView1);
//length of timer
new CountDownTimer(30000, 1000) {
//counts the milliseconds
public void onTick(long millisUntilFinished) {
//displays seconds remaining
text1.setText("seconds remaining: " + millisUntilFinished / 1000);
}
//displays when timer finishes
public void onFinish() {
text1.setText("done!");
} //<================================== error
this.start();
} //<============================== error
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The "start" call needs to be made on the object instance as follows:
(new CountDownTimer(30000, 1000) {
// your code here..
}).start();
In you code, the "this.start()" is within the class definition. It is not within any method.
Note that the "(" and ")" I have included are optional. I prefer to add them for clarity so that the object instance itself is contained. You could however do the following:
new CountDownTimer(30000, 1000) {
// your code here...
}.start();

Categories

Resources