Code Verified on-
Android Studio 2.1.2
Step1:
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 MyService.java
3. MainActivity.java
Android Studio 2.1.2
Step1:
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 MyService.java
package androidhubb.startedservice; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.support.annotation.Nullable; import android.widget.Toast; /** * Created by Supriya on 8/24/2016. */public class MyService extends Service{ @Override
public void onCreate() { super.onCreate(); } @Override
public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(getApplicationContext(),"Service started",Toast.LENGTH_LONG).show(); return START_STICKY; } @Override
public void onDestroy() { super.onDestroy(); Toast.makeText(getApplicationContext(),"Service stoped",Toast.LENGTH_LONG).show(); } @Nullable @Override
public IBinder onBind(Intent intent) { return null; } }
3. MainActivity.java
package androidhubb.startedservice; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override
protected 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