總網頁瀏覽量

2013年8月15日 星期四

Android(java) 使用cmd

Android中,透過APK直接使用cmd指令與device溝通

private Process process = null;

String[] cmd1 = { "/system/bin/sh", "-c", "echo hello > /data/temp.txt"};
process = Runtime.getRuntime().exec(cmd1);

String[] cmd2 = { "/system/bin/sh", "-c", "echo hello > /data/temp.txt"};

process = Runtime.getRuntime().exec(cmd2);

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;
            }
        }
    };
}

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();  
            }
           
        }  
    } 
}

Java 搜尋文字檔關鍵字並輸出

搜尋同一目錄內文字檔關鍵字後,並輸出整理到一個文字檔
可編譯為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++;
    }
}

2012年9月9日 星期日

Make file


Make file

基本說明
#include <stdio.h>
int main(void)
{
        printf("Hello World\n");
}
gcc hello.c  =>  產生a.out   輸入./a.out即可執行
gcc –c hello.c  =>  產生 hello.o 目的檔
gcc –o hello hello.o  =>  產生 hello 可執行檔  輸入./hello即可執行

-O:最佳化參數,-Wall:會產生詳細的編譯過程

gcc sin.c -lm -L/lib -L/usr/lib
-l :是『加入某個函式庫(library)』的意思,
 m :則是 libm.so 這個函式庫,其中, lib 與副檔名(.a .so)不需要寫
-L 後面接的路徑是剛剛上面那個函式庫的搜尋目錄
Linux 預設是將函式庫放置在 /lib /usr/lib 當中
-I/path 後面接的路徑( Path )就是設定要去搜尋相關的 include 檔案的目錄


製作Makefile
標的(target): 目標檔1 目標檔2
<tab>   gcc -o 欲建立的執行檔 目標檔1 目標檔2

main: main.o haha.o sin_value.o cos_value.o
           gcc -o main main.o haha.o sin_value.o cos_value.o -lm
clean:
           rm -f main main.o haha.o sin_value.o cos_value.o

LIBS = -lm
OBJS = main.o haha.o sin_value.o cos_value.o
CFLAGS=-Wall
main: ${OBJS}
        gcc -o main ${OBJS} ${LIBS}
clean:
        rm -f main ${OBJS}

$@:代表目前的標的(target)
gcc -o main ${OBJS} ${LIBS} 可改為gcc -o $@ ${OBJS} ${LIBS}


MakeFile

#Filename MakeFile
#this file is used for show how to use makefile
$(info start workoing)
hello: hello.c
    echo “nothing”

hello.bin: hello.c
    @echo “now make hello.bin”
    gcc hello.c –o hello.bin

.PHONY: he
    @echo “now make he”
    gcc hello.c –o hello.bin

$是函數呼叫符號, info是一個函數名稱,作用是輸出一段資訊,類似的資訊輸出還有warningerror兩個函數,不過error函數執行後會終止執行並退出
.PHONY關鍵字用於宣告一個目標,被.PHONY宣告的目標將總是執行其指定的命令,而如果不宣告的話,則僅當目標後面的條件變動後才執行
命令前面的@符號的作用是,不顯示被執行的命令,因為預設情況下,Make解譯器在執行命令時會列印出執行的命令
對於hello.bin目標,是當hello.c檔案被修改後,將會執行gcc命令重新對該c檔案編譯,並輸出hello.bin檔案

執行腳本,執行以下命令
$make  –f  Makefile  hello

-f參數用於指定要執行的指令檔案名稱,如果不指定檔案名稱,則解譯器會自動從目前的目錄下尋找名稱為Makefile的指令檔案