今回はアプリケーションサーバの代わりにブラウザを使ってメッセージを送信する方法について書いてみました。
2012年9月17日月曜日
2012年9月16日日曜日
2012年9月6日木曜日
ダブルクリックでジャンプできるようにログを出す方法
LogCatのダブルクリックでコードにジャンプする機能を利用する方法について考えてみたメモ
色々試した結果
at クラス名(ファイル名:ライン数)
がログに含まれているとLogCatでダブルクリックした時にコードにジャンプ出来るようです。
上記のコードではログの可読性向上のため、getPadding()でタブ数を計算してジャンプ用ログを整列しています。
こんな感じでログが出ます。
コード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Log; | |
public class DebugLog { | |
private static final String TAG = "MyApp"; | |
private static final int MAX_TAB_COUNT = 8; | |
private static final int TAB_SIZE = 4; | |
private static final int INDEX = 3; | |
/* | |
* DEBUG | |
*/ | |
public static void d(String message) { | |
if (BuildConfig.DEBUG) { | |
Log.d(TAG, message + getPadding(message) + getAdditionalLog()); | |
} | |
} | |
public static void d(String tag, String message) { | |
if (BuildConfig.DEBUG) { | |
Log.d(tag, message + getPadding(message) + getAdditionalLog()); | |
} | |
} | |
public static void d(String message, Throwable throwable) { | |
if (BuildConfig.DEBUG) { | |
Log.d(TAG, message + getPadding(message) + getAdditionalLog(), throwable); | |
} | |
} | |
public static void d(String tag, String message, Throwable throwable) { | |
if (BuildConfig.DEBUG) { | |
Log.d(tag, message + getPadding(message) + getAdditionalLog(), throwable); | |
} | |
} | |
private static String getPadding(String message) { | |
String padding = ""; | |
int tabCount = MAX_TAB_COUNT - (message.length() / TAB_SIZE); | |
if (tabCount <= 0) { | |
return "\t"; | |
} | |
for (int i = 0; i < tabCount; i++) { | |
padding += "\t"; | |
} | |
return padding; | |
} | |
private static String getAdditionalLog() { | |
String additionalLog = " at " + getClassName() + "#" + getMethodName() | |
+ "(" + getFileName() + ":" + getLineNumber() + ")"; | |
return additionalLog; | |
} | |
private static String getClassName() { | |
String className = new Throwable().getStackTrace()[INDEX].getClassName(); | |
className = className.substring(className.lastIndexOf(".") + 1); | |
return className; | |
} | |
private static String getMethodName() { | |
return new Throwable().getStackTrace()[INDEX].getMethodName(); | |
} | |
private static String getFileName() { | |
return new Throwable().getStackTrace()[INDEX].getFileName(); | |
} | |
private static String getLineNumber() { | |
return String.valueOf(new Throwable().getStackTrace()[INDEX].getLineNumber()); | |
} | |
} |
まとめ
色々試した結果
at クラス名(ファイル名:ライン数)
がログに含まれているとLogCatでダブルクリックした時にコードにジャンプ出来るようです。
上記のコードではログの可読性向上のため、getPadding()でタブ数を計算してジャンプ用ログを整列しています。
こんな感じでログが出ます。
2012年9月1日土曜日
ロックスクリーンを考慮して処理の再開を行う方法
この動画に「Lock Screen時は音楽再生を止める」というTipsがあったので、試してみたメモ
Google Developers Live
[JP 日本語] Google Play での Android アプリ提供ことはじめ
22分40秒あたり
コード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.app.KeyguardManager; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.os.Bundle; | |
import android.util.Log; | |
public class MainActivity extends Activity { | |
private final String TAG = getClass().getSimpleName(); | |
private MyBroadcastReceiver mReceiver = new MyBroadcastReceiver(); | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
protected void onResume() { | |
Log.d(TAG, "onResume()"); | |
super.onResume(); | |
IntentFilter intentFilter = new IntentFilter(); | |
intentFilter.addAction(Intent.ACTION_USER_PRESENT); | |
registerReceiver(mReceiver, intentFilter); | |
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); | |
Log.d(TAG, "inKeyguardRestrictedInputMode() : " + keyguardManager.inKeyguardRestrictedInputMode()); | |
if (!keyguardManager.inKeyguardRestrictedInputMode()) { | |
Log.d(TAG, "再開"); | |
/* | |
* 再開処理 | |
*/ | |
} | |
} | |
@Override | |
protected void onPause() { | |
Log.d(TAG, "onPause()"); | |
super.onPause(); | |
unregisterReceiver(mReceiver); | |
Log.d(TAG, "中断"); | |
/* | |
* 中断処理 | |
*/ | |
} | |
private class MyBroadcastReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Log.d(TAG, "MyBroadcastReceiver#onReceive()"); | |
Log.d(TAG, "再開"); | |
/* | |
* 再開処理 | |
*/ | |
} | |
} | |
} |
まとめ
- アプリが開いている状態でスリープから復帰した場合、ロックスクリーンが表示されていても、onResume()が呼ばれる
- ロックスクリーンが表示されているかの判定はKeyguardManager#inKeyguardRestrictedInputMode()で可能
- ロックスクリーンを使用していない場合への考慮も必要
- ロックスクリーン解除時は、ブロードキャストインテントandroid.intent.action.USER_PRESENTがシステムから通知される
その他
KeyguardManagerですが、API Level 16からisKeyguardLocked()/isKeyguardSecure()が追加されたようです。
isKeyguardLocked()でもロックスクリーン表示の判断が行えました。
isKeyguardSecure()はロックスクリーンにパスワードやパターンなどが設定されている時にtrueを返すようです。
※ API Level 15のIS11LGでも上記のメソッドが使用出来ました。リファレンスの誤記でしょうか?
参考にしたサイト
Yukiの枝折
Android:キーガードはActivityではなくViewであることの影響
登録:
投稿 (Atom)