總網頁瀏覽量

2013年1月28日 星期一

Android suspend resume code

使用service定時發出broadcast,當接收者收到intent後,即啟動resume,隔15sec後release wakelock,讓系統自動進入sleep狀態

MainActivity.java
package com.example.alarmmain;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
    Receive receive = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Intent intent = new Intent(MainActivity.this, MainService.class);
        startService(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void onPause() {
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}


MainService.java
package com.example.alarmmain;

import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MainService extends Service{
    private AlarmManager am;
    public void onCreate(){
        super.onCreate();
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

        Intent it = new Intent();
        it.setAction("com.asus.alarmWake");
        
        PendingIntent sender = PendingIntent.getBroadcast(MainService.this, 0, it, 0);
        am = (AlarmManager)getSystemService(ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30000, sender);
        Toast.makeText(getApplicationContext(), "Alarm Send",Toast.LENGTH_LONG).show();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }
}


Receive.java
package com.example.alarmmain;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.widget.Toast;

public class Receive extends BroadcastReceiver {
    private Handler handler = null;
    private PowerManager pm;
    private WakeLock wakeLock;
    
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "onReceive",Toast.LENGTH_LONG).show();
        
        handler = new Handler();
        pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP, "MainService");
        handler.postDelayed(wake, 0);

    }
    
    private Runnable wake = new Runnable() {
        public void run() {
            wakeLock.acquire();
            handler.postDelayed(sleep, 15000);
        }
    };
    private Runnable sleep = new Runnable() {
        public void run() {
            if (wakeLock != null && wakeLock.isHeld()) {
                wakeLock.release();
                wakeLock = null;
            }
        }
    };
}

沒有留言:

張貼留言