Code Verified on-
Android Studio 2.1.2
Step1: main_activity.xml
Take a new Project and open activity_main.xml file. Coding will be like following.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="androidhubb.setvices.MainActivity"
android:layout_margin="16dp"
android:gravity="center_vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Services"
android:onClick="onStart"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop Services"
android:onClick="onStop"/>
</LinearLayout>
Step 2:- To start a Bound Service we have to take another java file MyService.java | code will be like..
Android Studio 2.1.2
Step1: main_activity.xml
Take a new Project and open activity_main.xml file. Coding will be like following.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="androidhubb.setvices.MainActivity"
android:layout_margin="16dp"
android:gravity="center_vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Services"
android:onClick="onStart"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop Services"
android:onClick="onStop"/>
</LinearLayout>
Step 2:- To start a Bound Service we have to take another java file MyService.java | code will be like..
package androidhubb.boundservice; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; import android.support.annotation.Nullable; /** * Created by Supriya on 8/24/2016. */
public class MyService extends Service { MediaPlayer mp; @Nullable @Override
public IBinder onBind(Intent intent) { return null; } @Override
public void onCreate() { super.onCreate(); mp=MediaPlayer.create(this,R.raw.sanam); mp.setLooping(true); } @Override
public void onStart(Intent intent, int startId) { super.onStart(intent, startId); mp.start(); } @Override
public void onDestroy() { super.onDestroy(); mp.stop(); } }
3. MainAvtivity.java
package androidhubb.boundservice; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onStart(View v){ startService(new Intent(getApplicationContext(),MyService.class)); } public void onStop(View v){ stopService(new Intent(getApplicationContext(),MyService.class)); } }
No comments:
Post a Comment