測試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();
}
}
}
}
2012年10月9日 星期二
Java 搜尋文字檔關鍵字並輸出
搜尋同一目錄內文字檔關鍵字後,並輸出整理到一個文字檔
可編譯為jar檔,重複使用
Source code:
可編譯為jar檔,重複使用
Source code:
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.regex.Matcher; public class FindKeyWord { public static void main(String args[]) throws IOException{ ReadClass mReadClass = new ReadClass(); mReadClass.readData(); } } class ReadClass{ int count = 0; public void readData(){ //String dirPath = "C:\\Users\\Test\\Desktop\\"; String dirPathTemp = System.getProperty("user.dir"); String dirPath = Matcher.quoteReplacement(dirPathTemp); CharSequence findWord = "keyword"; File dir = new File(dirPath); StringBuilder sb = new StringBuilder(); if(dir.exists() && dir.isDirectory()){ String[] names = dir.list(); for(String name: names){ FileReader fr; try { fr = new FileReader(dirPath + "\\" + name); BufferedReader bf = new BufferedReader(fr); String data = null; try { while((data = bf.readLine()) != null){ if(data.contains(findWord)){ String regString = "="; String[] StringArray = data.split(regString); output(StringArray[1], dirPath); } } } catch (IOException e) { e.printStackTrace(); } try { bf.close(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } } }else{ String info = "路徑錯誤: " + dirPath + "請重新輸入"; } } public void output(String str, String dirPath) throws IOException{ String filePath = dirPath + "\\" + "data.txt"; FileWriter mFileWriter = new FileWriter(filePath, true); String item = null; if((count/10) < 1){ item = " " + Integer.toString(count); }else if((count/10) < 10){ item = " " + Integer.toString(count); }else{ item = Integer.toString(count); } String mString = new String(item + ", keyword = " + str + "\r\n"); mFileWriter.write(mString); mFileWriter.flush(); mFileWriter.close(); count++; } }
訂閱:
文章 (Atom)