package com.example.tutorialspoint7.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
public class MainActivity extends Activity {
String msg = "Android : ";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
}
public void startService(View view) {
startService(new Intent(getBaseContext(), MyService.class));
}
// Method to stop the service
public void stopService(View view) {
stopService(new Intent(getBaseContext(), MyService.class));
}
}
مثال:
این مثال، از طریق تعدادی مراحل ساده به شما آموزش می دهد که چگونه سرویس اندرویدی خود را ایجاد کنید. مراحل زیر را برای تغییر اپلیکیشن اندرویدی ای که ما در بخش Hello World Example ایجاد کردیم، دنبال کنید.
مرحله |
توصیف |
1 |
از Eclipse IDE برای ایجاد یک اپلیکیشن اندرویدی استفاده کنید و آن را همانطور که در مثال بخش Hello World Example شرح داده شد، تحت پکیج Hello World Example، HelloWorld بنامید. |
2 |
اینفایل فعالیت اصلی MainActivity.java را برای اضافه کردن متدهای startService() و stopService() تغییر دهید. |
3 |
یک فایل جاوای جدید MyService.java را تحت پکیج com.example.helloworld ایجاد کنید. این فایل شامل پیاده سازی متدهای مربوط به سرویس اندرویدی خواهد بود. |
4 |
در اثر اینسرویس خود را با استفاده از تگ ، در فایل AndroidManifest.xml تعریف کنید. یک اپلیکیشن بدون هیچ محدودیتی می تواند یک یا تعداد بیشتری سرویس داشته باشد. |
5 |
محتوای پیش فرض فایل فایل res/layout/activity_main.xml را تغییر دهید به طوریکه شامل دو دکمه در صفحه بندی خطی باشد. |
6 |
دو ثابت start_service و stop_service را در فایل res/values/strings.xml تعریف کنید. |
7 |
برای راه اندازی امولاتور اندروید اپلیکیشن را اجرا کنید و نتایج تغییرات اعمال شده در اپلیکیشن را بررسی کنید. |
در بخش زیر محتوای فایل فعالیت اصلی src/com.example.helloworld/MainActivity.java نمایش داده شده است. این فایل می تواند شامل هر کدام از متدهای چرخه حیات اصلی باشد. ما متدهای startService() و stopService() را برای آغاز کردن و توقف سرویس اضافه کرده ایم.
در بخش زیر محتوای فایل src/com.example.helloworld/MyService.java ارائه شده است. این فایل می تواند، شامل پیاده سازی یک یا تعداد بیشتری سرویسِ مربوط به Service اصلی یا نیازمندی ها باشد. در حال حاضر ما فقط قصد داریم که متدهای onStartCommand() و onDestroy() را پیاده سازی کنیم.
package com.example.tutorialspoint7.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
/**
* Created by TutorialsPoint7 on 8/23/2016.
*/
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}
در بخش زیر محتوای تغییر یافته ی فایل AndroidManifest.xml ارائه شده است، در اینجا ما تگ را برای وارد کردن سرویس مان اضافه کرده ایم.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tutorialspoint7.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" />
</application>
</manifest>
بخش زیر نشان دهنده ی محتوای فایل res/layout/activity_main.xml برای وارد کردن دو دکمه است.
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example of services"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials point "
android:textColor="#ff87ff09"
android:textSize="30dp"
android:layout_above="@+id/imageButton"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/abc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:text="Start Services"
android:onClick="startService"
android:layout_below="@+id/imageButton"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Services"
android:id="@+id/button"
android:onClick="stopService"
android:layout_below="@+id/button2"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2" />
</RelativeLayout>
اکنون می خواهیم اپلیکیشن تغییر یافته ی Hello World! را اجرا کنیم، من فرض می کنم که شما ADV خود را در طول نصب محیط ایجاد کرده باشید. برای اجرای اپلیکیشن از Eclipse، یکی از فایل های فعالیت پروژه ی خود را باز کنید و بر روی آیکن Run از نوار ابزار خود کلیک کنید. Eclipse، اپلیکیش را بر روی AVD شما نصب می کند و آن را آغاز می کند و در صورتی که همه ی موارد مربوط به تنظیمات و برنامه شما درست باشد، صفحه امولاتور زیر برای شما نمایش داده می شود.

اکنون برای آغاز کردن سرویس خود، بر وری دکمه Start Service کلیک کنید، این کار سرویس شما را آغاز می کند و با توجه به کدهای برنامه نویسی شده در متد onStartCommand() پیغام Service Started (به معنای سرویس آغاز شد)، در بخش انتهایی شبیه ساز به صورت زیر نمایش داده می شود.

برای متوقف کردن سرویس، شما می توانید بر روی دکمه Stop Service کلیک کنید.