Android-03-BackgroundJobs

IntentService

IntentService的一些限制

  • 无法直接与界面进行交互,必须把交互信息发到Activity中
  • 必须顺序地执行每一个任务
  • Service中的后台操作无法被中止

实现方法

1. 继承于IntentService

public class RSSPullService extends IntentService {
    @Override
    protected void onHandleIntent(Intent workIntent) {
        // Gets data from the incoming Intent
        String dataString = workIntent.getDataString();
        ...
        // Do work here, based on the contents of dataString
        ...
    }
}
2. 修改Manifest
<service
    android:name=".RSSPullService"
    android:exported="false"/>
3. 调用Intent
mServiceIntent = new Intent(getActivity(), RSSPullService.class);
mServiceIntent.setData(Uri.parse(dataUrl));

// Starts the IntentService
getActivity().startService(mServiceIntent);

4. 使用Broadcasts来进行通信

CursorLoader

实现方法

1. 实现相关接口

public class PhotoThumbnailFragment extends FragmentActivity implements
        LoaderManager.LoaderCallbacks<Cursor> {
...
}

2. 调用查询

private static final int URL_LOADER = 0;
getLoaderManager().initLoader(URL_LOADER, null, this);

3. 实现相关操作

public Loader<Cursor> onCreateLoader(int loaderID, Bundle bundle)
{
    /*
     * Takes action based on the ID of the Loader that's being created
     */
    switch (loaderID) {
        case URL_LOADER:
            // Returns a new CursorLoader
            return new CursorLoader(
                        getActivity(),   // Parent activity context
                        mDataUrl,        // Table to query
                        mProjection,     // Projection to return
                        null,            // No selection clause
                        null,            // No selection arguments
                        null             // Default sort order
        );
        default:
            // An invalid id was passed in
            return null;
    }
}

管理设备唤醒

屏幕常亮

1. 方法一:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// clear the flag to turn the off the screen
<code>// getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)</code>

2. 方法二:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true">
    ...
</RelativeLayout>

保持CPU运行

注意保持CPU运行有可能会降低电池寿命

方法: 首先设置权限,然后再调用powerManager

<uses-permission android:name="android.permission.WAKE_LOCK" />

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
Wakelock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
        "MyWakelockTag");
wakeLock.acquire();
<code>// wakelock.release()</code>;

闹钟

闹钟的一些特点

  • 允许你在某一时间启动一个Intent
  • 可以结合broadcast来启动service或实现其他的一些操作
  • 他在你的应用外运行,因此既使你的应用没有运行,他也可以触发一些事件或动作
  • 他可以提高资源的利用率,你无需手动去做一个时间记录或在一个Service中来不断地运行一些工作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值