android in practice_create and start a service(portfolio project)
来源:互联网 发布:no sleep mac下载 编辑:程序博客网 时间:2024/06/08 20:03
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);
}
}
- android in practice_create and start a service(portfolio project)
- android in practice_Communicating with a Service (portfolio project)
- android in practice_Using a Service for caching data(portfolio project)
- android in practice_create model、table class and sqlitehelper(MyMoviesDataBases project)
- android in practice_create model、table class and sqlitehelper(MyMoviesDataBases project)
- android in practice_Creating notifications(portfolio project)
- android in practice_Using the AlarmManager(portfolio project)
- android in practice_Keeping Services awake(portfolio project)
- android in practice_Using Cloud to Device Messaging(portfolio project)
- Project Server 2007 Queue Service and Event Service could not start
- cocos2dx linux and android project in eclipse
- linux: start a service
- IT Project Portfolio Management
- lighttpd start and running a cgi written in C
- how to start and stop android service by using adb
- In eclipse: A child container failed during start and Failed to start components
- android service 之一 (start service)
- android service 之一 (start service)
- 设计模式——Iterator模式实现研究
- 源码结构
- x264中x264_cpu_detect函数欣赏
- cocos2d-x SimpleGame入门例程笔记
- LINQ to SQL语句(1)之Where
- android in practice_create and start a service(portfolio project)
- 八大經方治感冒
- LINQ to SQL语句(2)之Select/Distinct
- Iterator模式
- XmlDocument to Byte[] and bytes[] to xmldocument or xdocument.
- add-apt-repository用法
- 进程同步互斥——读者写者问题
- vim - cannot save backup file
- LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg