如何在活动之外创建多个活动实例

时间:2014-07-26 12:12:42

标签: android geofencing

嗨,搜索后,我无法得到如何创建多个活动实例的答案。

1.我有一个广播接收器处理谷歌播放服务发送的传入位置围栏。

2.Next在广播接收器中,它会在这里收到一组Geofence我需要启动相同活动的多个实例,但实际上我无法实现。

我开始删除下面的代码:

public class GeoFenceReceiver extends BroadcastReceiver {

private Context mContext;

private HistoryDAO mHistoryDAO;

public GeoFenceReceiver() {
}

private Context getContext(){
    return mContext;
}

private void initializeHistoryDAO() {
    if(mHistoryDAO == null){
        mHistoryDAO = new HistoryDAO(getContext());
    }
}

private boolean hasDataAccess(){
    return mHistoryDAO != null;
}

private HistoryDAO getHistroyDAO(){
    if(!hasDataAccess()){
        initializeHistoryDAO();
    }
    return mHistoryDAO;
}

private HistoryData getRawData(int rowID){
    List<HistoryData> listDatas = getHistroyDAO().getHistoryById(rowID);
    if (listDatas != null && listDatas.size() > 0) {
        return listDatas.get(0);
    }
    return null;
}

private void releaseDataAccess(){
    if(hasDataAccess()){
        mHistoryDAO.releaseHelper();
    }
}

private String getString(int resID,Object... formatArgs){
    return getContext().getString(resID,formatArgs);
}

@Override
public void onReceive(Context context, Intent intent) {

    mContext = context;

    //set up the dao
    initializeHistoryDAO();

    //get intent action
    String action = intent.getAction();

    // Handle the intent send by google play service for receive the geo fence transition 
    if(TextUtils.equals(action,GeofenceUtils.ACTION_GEOFENCE_RECEIVED)){

        handleReceiveGeofence(context, intent);

    } else if (TextUtils.equals(action, SnoozeUtils.ACTION_SNOOZE_RECEIVER)) {

        // handle the incomming intent send by system alarm manager
        // This intent contain the reminder id/ history plain java object in
        // string json format that we send it to the
        // pop up of reminder for show the reminder details.

        handleSnoozeAction(context, intent);
    }
    //release data connection
    releaseDataAccess();


}

参见上面的 handleReceiveGeofence(context,intent); ,当我们收到我们在这里处理的新击剑时。

 /**
 * 
 * @param context
 * @param intent
 */
private void handleReceiveGeofence(Context context, Intent intent){
    mContext = context;

    // Create a local broadcast Intent
    Intent broadcastIntent = new Intent();

    // Give it the category for all intents sent by the Intent Service
    broadcastIntent.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);

     // First check for errors
    if (LocationClient.hasError(intent)) {

       // handle any error code here...

    // If there's no error, get the transition type and create a notification
    } else {

        //play notification sound
        notificationSound();

        //vibrart the device 
        notificationVibration();

        // Get the type of transition (entry or exit)
        int transition = LocationClient.getGeofenceTransition(intent);

        // Test that a valid transition was reported
        if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER)
                ||(transition == Geofence.GEOFENCE_TRANSITION_EXIT)) {

            //transition type 
            String transitionType = getTransitionString(transition);

            // Post a notification
            List<Geofence> geofences = LocationClient.getTriggeringGeofences(intent);

            String[] geofenceIds = new String[geofences.size()];

            for (int index = 0; index < geofences.size() ; index++) {
                geofenceIds[index] = geofences.get(index).getRequestId();
                showNotification(transition,String.valueOf(geofences.get(index).getRequestId()));
            }
            // geofence id 
            String ids = TextUtils.join(GeofenceUtils.GEOFENCE_ID_DELIMITER,geofenceIds);

            if(BackAppApplication.getInstance().getAppDebugMode()){
                sendNotification(transitionType, ids);  
            }
            // Log the transition type and a message
            Log.d(GeofenceUtils.APPTAG,getString(R.string.geofence_transition_notification_title,transitionType,ids));

            Log.d(GeofenceUtils.APPTAG,getString(R.string.geofence_transition_notification_text));

        // An invalid transition was reported
        } else {
            // Always log as an error
            Log.e(GeofenceUtils.APPTAG,getString(R.string.geofence_transition_invalid_type, transition));
        }
    }
}

3.现在在上面的代码中看到 List geofences = LocationClient.getTriggeringGeofences(intent);

上面的列表现在包含地理围栏详细信息的集合如果它包含单个项目,那么我有

显示/启动活动没有问题它工作正常。

但是当多个地理围栏进入广播意图时就会出现问题。它只显示一个

活动的实例而不是活动的堆栈。

4.下一步显示活动的主要区域。

public synchronized void showNotification(final int transitionType,final String reminderID){

    // we check here whether the reminder id exits with active on or off
    HistoryData mData = getRawData(Integer.parseInt(reminderID));
    if (mData != null && mData.isActive()) {

        //check the transition type if it is enter check the grace time
        if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER) {

            if (Utility.checkDateDifferent(mData.getTimeStamp()) < GeofenceUtils.MIN_PROXALERT_INTERVAL) {

                Intent mIntent = new Intent(getContext(),TransitionErrorPopUp.class);
                mIntent.putExtra(AbsReminderPopUp.EXTRA_REMINDER_ID,reminderID);
                mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
                getContext().startActivity(mIntent);
                return ;
            }
        }

        Intent mIntent = new Intent(getContext(),ReminderPopUp.class);
        mIntent.putExtra(AbsReminderPopUp.EXTRA_REMINDER_ID,reminderID);
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        getContext().startActivity(mIntent);

    }
}

5.我已经尝试删除FLAG_ACTIVITY_MULTIPLE_TASK

6.据我所知,从活动组件外部开始活动需要设置标志

FLAG_ACTIVITY_NEW_TASK。

我在过去三天遇到这个问题我最后在这里发帖可能有些人可以帮助我。

0 个答案:

没有答案