现在的位置: 首页 > 综合 > 正文

android in practice_create and start a service(portfolio project)

2018年04月13日 ⁄ 综合 ⁄ 共 3047字 ⁄ 字号 评论关闭

Main Application, Main Process, Main Service,Main Task,Main Activity vs Multiply Applications,Multiply Processes,Multiply Services,Multiply Tasks, Multiply Activities

The way to fully implement multitasking on Android is by using Services.and so on.

If you need to start a task separate from your main application, consider using a Service.and so on.

project StockPortfolio

(1)fetch the latest stock data in the background and cache it locally

(2)compare the current stock prices to see if they’re at a level where the user wants to receive a notification.

Creating a Service

(1)To create a Service, you’ll need to declare it in your manifest file.

AndroidMainifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example.stockportfolio"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ViewStocks"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:process=":stocks_background"
            android:name="PortfolioManagerService"

            android:icon="@drawable/icon"
            android:label="@string/service_name"
            />
    </application>
</manifest>

specifying the OS-level process that the Service will run in.if you don’t specify it, then the Service will run in the same process as your application.The colon prefix is significant—it indicates that this separate process
is private to the application.if not it would be a global process.

    Declare the Service’s class  PortfolioManagerService.

Declaring the PortfolioManagerService

public class PortfolioManagerService extends Service {
/*
* a Service usually run in its own process.Interprocess communication (IPC) is necessary.
* The onBind method is where the IPC channel is established.
*/
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}

Starting a Service automatically

Use a BroadcastReceiver to listen for Android’s BOOT_COMPLETED event.

Declaring a BroadcastReceiver for the boot complete event in AndroidMainifest.xml:

 <receiver android:name="PortfolioStartupReceiver"
            android:process=":stocks_background" >
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
  </receiver>

class name:PortfolioStartupReceiver.

run in process:stocks_background.

event to listen for:BOOT_COMPLETED

Starting our Service with a BroadcastReceiver

public class PortfolioStartupReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
/*It creates a new Intent and uses that Intent to start the Service.
* This will cause the onCreate and then the onStartCommand methods to be invoked on our Service(PortfolioManagerService), 
* and then return back to the BroadcastReceiver.
*/
Intent stockService=new Intent(context,PortfolioManagerService.class);
context.startService(stockService);
}
}


抱歉!评论已关闭.