Domain For Sale 1000$
How to Create a splash screen on android
Create a class called: Splash then
paste this code into the flowing class
change the replace your main class by ".Splash" (the name of class we just created) then declare a new activity with the name of de fault class you just created
public class Splash extends Activity
{
boolean flag = false;
Runnable runnable;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
runnable = new Runnable() {
@Override
public void run() {
if (!flag) {
final Intent mainIntent = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}
};
handler = new Handler();
handler.postDelayed(runnable, 5000);
}
@Override
protected void onPause() {
super.onPause();
flag = true;
handler.removeCallbacks(runnable);
}
@Override
protected void onRestart() {
super.onRestart();
flag = false;
handler.postDelayed(runnable, 1-1);
}
}
change the replace your main class by ".Splash" (the name of class we just created) then declare a new activity with the name of de fault class you just created
finally create your activity_splash layout in the layout folder
How to play YouTube video in Android application ?
As we know playing a video stored in user device will never be a solutions specially if you app contains a large amount of information’s like a city guide, e-learning app... and so on and storing it locally we surely abuse the user and push him to uninstalled. So in this tutorial we are going to play a YouTube video with tow different methods and we are going also to play a video from an SDCARD as a demonstration of the Video View component
Very important playing a video with android emulator will never work properly you must use a real android device for testing This method will launch the YouTube app and not in a videoview All you have to do is to paste this code inside the event of your button and don't forget to replace video_id by the located on the URL of YouTube video you want to play
String video_id = "2vFLW-idOxk";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + video_id);
startActivity(intent);
you can also use this method for clean and nice coding
private void startVideo(String videoID) {
// default youtube app
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + videoID));
List list = getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() == 0) {
// default youtube app not present or doesn't conform to the standard we know
// use our own activity
i = new Intent(getApplicationContext(), YouTube.class);
i.putExtra("VIDEO_ID", videoID);
}
startActivity(i);
}
Add the following permissions in the manifest file
How to check internet connection on android
One of the most basic thinks to learn before creating an application on android is to check if the internet available or not in order to download data like xml freed images YouTube video … and without it the application will crash or it will not run properly in order to solve this problem we are going to use net work class that will help us to check internet status
1
1 – Setup
Create a class called ConnectionDetector and copy and paste the following code in itpackage com.example.radionabeul;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
2 – How to use It
The isConnectingToInternet() inside our class ConnectionDetector will simply return true if the connection is on and false if there is no connection and we are done.1
//declare ConnectionDetector before onCreate method private ConnectionDetector cn;2 add this code to onClick method of your button
cn = new ConnectionDetector(getApplicationContext());
if (cn.isConnectingToInternet()) {
Toast.makeText(getApplicationContext(), "internet is on",
3000).show();
}
else {
Toast.makeText(getApplicationContext(), "internet is off",
3000).show();
}
please do not forget to add ACCESS_NETWORK_STATE and INTERNET permission in AndroidManifest file "AndroidManifest.xml"
and
Android radio streaming tutorial
Just add those components to your layout 1 – Button for play with the id: buttonPlay 2 – Button for stop with the id: buttonbuttonStopPlay 3-progressBar to show the buffing of the stream id: progressBar1
import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class myMain extends Activity implements OnClickListener {
private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initializeUIElements();
initializeMediaPlayer();
}
private void initializeUIElements() {
playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
playSeekBar.setMax(100);
playSeekBar.setVisibility(View.INVISIBLE);
buttonPlay = (Button) findViewById(R.id.buttonPlay);
buttonPlay.setOnClickListener(this);
buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
buttonStopPlay.setEnabled(false);
buttonStopPlay.setOnClickListener(this);
}
public void onClick(View v) {
if (v == buttonPlay) {
startPlaying();
} else if (v == buttonStopPlay) {
stopPlaying();
}
}
private void startPlaying() {
buttonStopPlay.setEnabled(true);
buttonPlay.setEnabled(false);
playSeekBar.setVisibility(View.VISIBLE);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
}
private void stopPlaying() {
if (player.isPlaying()) {
player.stop();
player.release();
initializeMediaPlayer();
}
buttonPlay.setEnabled(true);
buttonStopPlay.setEnabled(false);
playSeekBar.setVisibility(View.INVISIBLE);
}
private void initializeMediaPlayer() {
player = new MediaPlayer();
try {
player.setDataSource("http://usa8-vn.mixstream.net:8138");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {
public void onBufferingUpdate(MediaPlayer mp, int percent) {
playSeekBar.setSecondaryProgress(percent);
Log.i("Buffering", "" + percent);
}
});
}
@Override
protected void onPause() {
super.onPause();
if (player.isPlaying()) {
player.stop();
}
}
}
android manifest file :as you can see i have added a internet permission and without it the application will not work
Inscription à :
Articles (Atom)





