2013年4月27日土曜日

[AndroidSDK]ServiceのbindService()でfalseが帰る

Intent intent = new Intent(this, MyService.class);
boolean result = bindService(intent, mConnection, Context.BIND_AUTO_CREATE);.

でfalseが返ってくる

StackOverflowによると

Android Context.bindService always returns false and ServiceConnection object is never triggered
(bindServiceでfalseが返ってきてServiceConnectionが動かねえよ)


getApplicationContext().bindService()でいけるぜ、
この回答で動いた人がおおいみたいだが、今回はどうも違うようだ。

結論
Manifest.xmlで
<service android:name=".MyService" />
となっていたが、パッケージが間違っていた
<service android:name="Utility.MyService" />

2013年4月26日金曜日

[AndroidSDK]Google PlayにおけるAndroid端末のシェア率を見る

公式が出している。

Dashboards
http://developer.android.com/intl/ja/about/dashboards/index.html#Platform

14日前のデータとのこと。
これを見てどのバージョンに合わせるか考えると良い

iOSもこの表があると良いな、不定期にしか出してくれないからな

2013年4月24日水曜日

[AndroidSDK]AsyncTaskを使用してバックグランド処理を行う。

ネットワーク処理をさせている間にProgressDialogを表示させたい時など
public class MainActivity extends Activity {
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        progressDialog = new ProgressDialog(this);

        MyAsyncTask task = new MyAsyncTask();
        //バックグランド処理を実行
        task.execute(getApplicationContext());
        //このログが実行されているときに、MyAsyncTaskが終わっているとは限らなので
        //MyAsyncTask後に処理させたいときはonPostExecute()を使用すること
        Log.i("tag", "mes");
    }

    public class MyAsyncTask extends AsyncTask {
        @Override
        //doInBackgroundが実行されてる前に呼ばれる
        protected void onPreExecute() {
            //ProgressDialogなどはここで実装すると良い
            progressDialog.setMessage("データを取得しています。");
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        //バックグランド処理はここに追加
        @Override
        protected Integer doInBackground(Context... context) {
            //何かの処理

            return 1;
        }

        //doInBackgroundが実行された後に呼ばれる
        @Override
        protected void onPostExecute(Integer result) {
            progressDialog.dismiss();
        }
    }
}

2013年4月23日火曜日

[AndroidSDK]DefaultHttpClientを使ってPOSTをする

public void httpConnect() {
 DefaultHttpClient client = new DefaultHttpClient();
 StringBuilder uri = new StringBuilder("hostname.com");
 HttpPost http = new HttpPost(uri.toString());

 http.setHeader("Content-type", "text/plain; charset=utf-8");
 //bodyの作成
 try {
  http.setEntity(new StringEntity("body", "UTF-8"));
 } catch (UnsupportedEncodingException e) {
  e.printStackTrace();
 }

 HttpResponse httpResponse = null;
 try {
  // HTTPリクエストを送信
  httpResponse = client.execute(http);
 } catch (Exception e) {
  e.printStackTrace();
 }

 // HTTPレスポンスを取得
 int status = httpResponse.getStatusLine().getStatusCode();
 if (HttpStatus.SC_OK == status) {
  //ヘッダーの表示
  Header[] headerArray = http.getAllHeaders();
  for (Header header : headerArray) {
   Log.d(getLocalClassName(), "header[" + header.getName() + "] [" + header.getValue() + "]");
  }
  // Bodyの取得
  HttpEntity httpEntity = httpResponse.getEntity();
  String body = null;
  try {
   body = EntityUtils.toString(httpEntity);
  } catch (ParseException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  Log.d(getLocalClassName(), "body " + body);
 } else {
  //TODO エラー処理
 }
}

2013年4月22日月曜日

[Android SDK]Activity、アプリを終了させる。


finish()
でactivityを1つずつ終わらせるのがベスト
ただしスタックされたn個のActivityを終わらせるには、見た目上うまく表示する必要がある。

this.moveTaskToBack(true);
でアプリがバックグランドに入るので見た目上はこっちの方が良い

System.exit(int) 
は問題があるので使わない方がいい

2013年4月18日木曜日

[AndroidSDK]Thread時のUI更新

・AndroidアプリはUI更新は1タスクで回っている
・ThreadからUI更新をしても、更新先が無い
・Handlerで割り込みをして、UI更新のタスクに委任する

public class SampleActivity extends Activity {
 private static Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
  mHandler = new Handler(); //Handlerを作成
  
  //Threadを開始
  new Thread(new Runnable() {
                public void run() {
                
                if (true) { 
                 TextView textView = (TextView) findViewById(R.id.TextView);
                 textView.setText("Handlerを使わないとここで落ちる");
                }
                
    mHandler.post(new Runnable() {
     public void run() {
      TextView textView = (TextView) findViewById(R.id.TextView);
                  textView.setText("Handlerを使って更新するので落ちるない");
     }
    });
   }
  }).start();
 }
}

参考サイト
TechBooster
http://techbooster.org/android/application/6191/

throw Life
http://www.adamrocker.com/blog/261/

2013年4月15日月曜日

[Android SDK]デバックモードの判別


[Android SDK]デバックモードの判別

Debug.isDebuggerConnected()

で判定できる。

manifest.xmlで
android:debuggable="true"
とすると。
Avoid hardcoding the debug mode; leaving it out allows debug and release builds to automatically assign one
とワーニングが出るが、debuggableは自動的に判別するのでmanifest.xmlで設定をする必要がない、
eclipseからの起動でdebug=trueとなり、apkを書きだす時にdebug=falseになる。