總網頁瀏覽量

2012年10月9日 星期二

Android 測試Android ProximityAlertReciever類別

測試Android ProximityAlertReciever類別
設定好監聽事件後,再更新座標,並接收警告或通知



Source code:

package com.example.addproximityalert;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private static final String PROXIMITY_ALERT_ACTION_NAME = "com.test"; 
    private static final String TEST_MOCK_PROVIDER_NAME = "test_provider";
    private Button btnOut, btnIn;
    private TextView longitude_txt, latitude_txt;
    private LocationManager mLocationManager;
    private ProximityAlertReciever par;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        String locService = Context.LOCATION_SERVICE;   
        mLocationManager = (LocationManager) getSystemService(locService); 
        par = new ProximityAlertReciever();
        LocationProvider lp = mLocationManager.getProvider(TEST_MOCK_PROVIDER_NAME);
        if (lp != null) {
            mLocationManager.removeTestProvider(TEST_MOCK_PROVIDER_NAME);
        }
        addTestProvider(TEST_MOCK_PROVIDER_NAME);
        set();

    }
    public void findViews(){
        longitude_txt = (TextView)findViewById(R.id.longitude);
        latitude_txt = (TextView)findViewById(R.id.latitude);
        
        btnOut = (Button) findViewById(R.id.btn1); 
        btnOut.setOnClickListener(new OnClickListener() {  
            public void onClick(View arg0) {  
               updateLocation(30, 30);   
            }  
        });
        
        btnIn = (Button) findViewById(R.id.btn2); 
        btnIn.setOnClickListener(new OnClickListener() {  
            public void onClick(View arg0) {  
               updateLocation(0, 0);   
            }  
        });
    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    private void updateLocation(final double latitude, final double longitude){
        updateLocation(TEST_MOCK_PROVIDER_NAME, latitude, longitude);
    }
    
    private void updateLocation(final String providerName, final double latitude, final double longitude) {
        Location location = new Location(providerName);
        location.setLatitude(latitude);
        location.setLongitude(longitude);
        location.setTime(java.lang.System.currentTimeMillis());
        mLocationManager.setTestProviderLocation(providerName, location);
    }
    
    private void set(){
        double lat = 0;  
        double lng = 0;   
        float radius = 1000;    
        long expiration = -1;  
        Intent intent = new Intent(PROXIMITY_ALERT_ACTION_NAME);

        PendingIntent pi = PendingIntent.getBroadcast(this, -1, intent, PendingIntent.FLAG_ONE_SHOT);  
        mLocationManager.addProximityAlert(lat, lng, radius, expiration, pi);  
        IntentFilter filter = new IntentFilter(PROXIMITY_ALERT_ACTION_NAME);
        registerReceiver(new ProximityAlertReciever(), filter);
    }
    
    private void addTestProvider(final String providerName) {
        mLocationManager.addTestProvider(providerName, true, false, true, false, false, false, false, Criteria.POWER_MEDIUM, Criteria.ACCURACY_FINE);
        mLocationManager.setTestProviderEnabled(providerName, true);
    }
    
    
    class ProximityAlertReciever extends BroadcastReceiver {    
        @Override  
        public void onReceive(Context context, Intent intent) {    
            String key = LocationManager.KEY_PROXIMITY_ENTERING;  
            boolean isEnter = intent.getBooleanExtra(key, false);  
            if(isEnter){
                Toast.makeText(context, "以進入區域", Toast.LENGTH_LONG).show();  
            }
           
        }  
    } 
}

1 則留言:

  1. 請問一下 可以提供一下 androidManifest.xml檔案看一下嗎?
    謝謝

    回覆刪除