从broadcastreceiver开始活动和服务

时间:2011-04-27 17:35:04

标签: android

首先抱歉我的英语。所以我正在制作一个程序,在他/她需要时提醒用户 进入他/她的药物。我的基本想法:

1 AlarmManager向我发送信号
2我用我的广播接收器捕捉到这个信号,
3,广播接收器启动我的服务和我的活动。

从10分钟到每分钟的服务倒计时将检查数据库中的其他参数 并且活动将发出声音和振动。因为我会列出更多药物 纽扣。并且因为服务和活动使用相同的游标,所以服务与之连接 数据库。所以当我的Activity开始时,它需要从服务中获取光标(对于cursoradapter)。 如果服务或活动改变了光标我需要刷新光标,所以我在服务中进行重新查询。

唯一的问题是虽然我的bindService给了我真实的结果,我的onServiceConnected 方法没有运行,所以我得到一个nullpointer异常,当我尝试调用公共服务方法(对于游标) 从我的活动。我尝试在onStartCommand之后从服务启动我的Activity,但结果也是一个nullpointer。

实现任务的最佳方法是什么? 我应该删除我的服务并从将执行服务工作的活动中创建另一个线程(AsyncTask)吗?

好的,我试图抓住重要部分。

package elte.thesis.broadcastreceivers;

import elte.thesis.alarmsmanager.AlarmActivity;
import elte.thesis.alarmsmanager.WakeLockManager;
import elte.thesis.database.Utility;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/*
 * This BroadcastReceiver will receive the ALARM from the AlarmManager
 * and it will 
 * 1, start the Service and 
 * 2, show the AlarmActivity 
 */
public class DosageAlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.v("Szakbarbar",
            "AlarmManager send me the next " + "alarm time: " +    intent.getStringExtra("AlarmTime"));

    /*
     * It seems that the alarmmanager release the wakelock as soon as our
     * onReceive method return, so we need to make our own wakelock
     */
    WakeLockManager.acquireCpuWakeLock(context);

    /*
     * Start the service and give him the time what we got from alarm manager
     */
    Log.v("Szakbarbar", "Elinditjuk a servicunket");
    Intent serviceIntent = new Intent(Utility.ALARM_SERVICE);
    serviceIntent.putExtra("AlarmTime", intent.getStringExtra("AlarmTime"));
    context.startService(serviceIntent);

    /*
     * Start the AlarmActivity where the user can sign that he got in the
     * pills
     */
    Log.v("Szakbarbar", "Elinditjuk az activitinket");
    Intent alarmActivity = new Intent(context, AlarmActivity.class);
    alarmActivity.putExtra("AlarmTime", intent.getStringExtra("AlarmTime"));
    alarmActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
    context.startActivity(alarmActivity);

}

 }

在我的AlarmActivity中

 AlarmService mService;
 ...
@Override
public void onStart() {
    super.onStart();

    Log.v("Szakbarbar", "Bind to the Service");
    Intent intent = new Intent(Utility.ALARM_SERVICE);
    boolean connection = bindService(intent, mConnection,      Context.BIND_AUTO_CREATE);
    Log.v("Szakbarbar", "Binded to the Service : " +      ((connection==true)?"kapcsolt":"nemkapcsolt"));
}

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

    Log.v("Szakbarbar", "Need the cursor from the Service");
    updateDosageAdapter();

    //Make one private register to now when the AlarmService made change on the cursor
    registerReceiver(mReceiver, new IntentFilter(Utility.ALARMS_CHANGED));

    if (!mute) {
        if (volume < 1)
            incAlarmVolume();
        mVibrator.vibrate(sVibratePattern, 0);
        player.start();
    }
}

/** Defines callbacks for service binding, passed to bindService() */
private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className, IBinder service) {
        // We've bound to LocalService, cast the IBinder and get LocalService instance
        Log.v("Szakbarbar", "mService itt még nulla");
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;
        Log.v("Szakbarbar", "mService itt már nem nulla");          
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
};
public void updateDosageAdapter() {
    Log.v("Szakbarbar", "1");
    alarmsCursor = mService.refreshCursor();
    Log.v("Szakbarbar", "2");       
    startManagingCursor(alarmsCursor);
    Log.v("Szakbarbar", "3");       
    if (alarmsAdapter == null) {
        Log.v("Szakbarbar", "4");           
        alarmsAdapter = new AlarmsAdapter(this, alarmsCursor);
        alarmsList = (ListView) findViewById(R.id.dosage_alarm_list);
        alarmsList.setAdapter(alarmsAdapter);
        alarmsList.setVerticalScrollBarEnabled(true);
        alarmsList.setScrollContainer(false);
        alarmsList.setOnCreateContextMenuListener(this);
    } else {
        Log.v("Where is nullpointerexception", "5");            
        alarmsAdapter.changeCursor(alarmsCursor);
    }
}

在我的服务中

 private final IBinder mBinder = new LocalBinder();
 ...
// Return this instance of AlarmService so clients can call public methods
public class LocalBinder extends Binder {
    AlarmService getService() {
        return AlarmService.this;
    }
}

@Override
public IBinder onBind(Intent intent) {
    return mBinder;
}

//Refresh the cursor
public Cursor refreshCursor() {

    Log.v("Szakbarbar", "ResreshCrusor : " + alarmTime);

    alarmCursor = Utility.selectDosageAlarms(alarmTime);        
    return alarmCursor;
}

我的问题的简短描述 我想你还不知道我的问题是什么。我不擅长英语,对不起有问题。虽然我在onStart()上调用了bindService() AlarmActivity的方法,当我调用(也来自AlarmActivity)updateDosageAdapter()时,使用我获得的服务中的refreshCursor()方法 一个空指针异常,因为onServiceConnected()方法还没有运行,我的mService varieble应该保持 我的服务的引用为空。

1 个答案:

答案 0 :(得分:1)

我设法找到了this post

中描述的解决方案