トップ «前月 最新 翌月»
【ソース+水=麦茶色の何か】

半期 四半期 全カテゴリ

今日の一言


2012/06/02(Sat) Mac mini買った。さらに、CFDのメモリを買って16Gにした。超快適。 [長年日記] 編集 17:00現在 23℃

_ [Mac] MacでWindowsキーボードの半角/全角漢字をつかう(KeyRemap4MacBook)

「KeyRemap4MacBook」(http://pqrs.org/macosx/keyremap4macbook/index.html.ja)というソフトをインストールすることで、半角/全角漢字キーに限らず、キーマップを自由に変更することが出来る。

ここ(http://pqrs.org/macosx/keyremap4macbook/index.html.ja)から自分の環境に合ったdmgファイルをダウンロードし、展開・実行・インストールする。

インストール完了後、再起動すると、システム環境設定に KeyRemap4MacBook が追加される。

Windowsキーボードの半角/全角漢字キーをかな/英数の切り替えに使えるようにするには、「Change Key」の「Remapping」から「For Japanese」を選び、「Change Backquote key」のBackquote to KANA/EISUU」にチェックを入れる。

その他の細かな使い方については、こちら(http://pqrs.org/macosx/keyremap4macbook/document.html.ja)にまとめられている。

以上。


2012/06/03(Sun) フラクタルって言われるほどひどくないよな? [長年日記] 編集 18:00現在 24℃

_ [Android][Linux][Mac][Ubuntu][Windows][Debian]EclipseでAndroidのプラグインを更新したらエラーになった場合

EclipseでAndroidのプラグインを更新した際に以下のエラーが出た場合。

【エラー文言】

com.android.sdkuilib.internal.widgets.SdkTargetSelector.access$000 (Lcom/android/sdkuilib/internal/widgets/SdkTargetSelector;)Z

これは、Eclipseをcleanオプション付きで起動させることで解消できる。

Windows:

"eclipse.exe -clean.cmd"

Linux, Mac:

"eclipse -clean"

2012/06/04(Mon) たこ焼きうまい。 [長年日記] 編集 22:00現在 21℃

_ [Android]AndroidでActivityより前に実施したい処理(Activityの生成に割り込みたい場合)の実装

参考:

 http://developer.android.com/reference/android/app/Application.html

Applicationクラスを拡張することで、Activity生成前に処理を行わせることが出来る。

初期設定ファイルを読み込ませたい場合などに便利。

【TestAppActivity.java】

package net.i.akihiro.TestApp;
import android.app.Application;

public class TestAppApplication extends Application {

@Override
public void onCreate() {
	super.onCreate();
}

@Override
public void onLowMemory() {
	super.onLowMemory();
	System.gc();
}

@Override
public void onTerminate() {
	super.onTerminate();
}
}

【AndroidManifest.xml】

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="net.i.akihiro.TestApp"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk android:minSdkVersion="7" />

   <application
       android:name=".TestAppApplication"
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name" >
       <activity
           android:name=".SampleAppActivity"
           android:label="@string/app_name" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
   </application>

</manifest>

2012/06/05(Tue) 牛乳うまい [長年日記] 編集 22:30現在 20℃

_ [Android]Androidでブラウザから自作アプリを呼び出す

ブラウザから呼び出したいアプリのAndroidManifest.xmlに、ブラウザから起動させるためのIntentFilterを設定すればいい。

<activity android:name=".TestAppActivity" android:label="@string/app_name" >
  <intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="testapp" android:host="start" android:pathPattern="/.*"/>
  </intent-filter>
</application>

上記の例では、ブラウザから「testapp://start/」というURLを開くことで「TestAppActivity」を開くことが出来るようになる。

また、pathに「android:pathPattern="/.*"/」を指定しているため、「testapp://start/hogehoge」のように、「testapp://start/」以下に自由な文字列を設定できる。

ただし、ブラウザによってはアドレスバーに上記のURLを入力しても検索になってしまうものがある。そのため、基本的には、ページ内にアプリ起動用のリンクを用意することになる。

また、端末が接続されており、adb shellが実行できる場合、コマンドプロンプトから以下のコマンドを打つことで、ブラウザから開くのと同じ暗黙的Intentを発行することが出来る。

> adb shell am start -a android.intent.action.VIEW testapp://start/

なお、呼び出しに使用したURLは以下のようにして取得することが出来る。

【TestAppActivity.java】

package net.i.akihiro.TestApp;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class TestAppActivity extends Activity {
private static final String LOG_TAG = "TestApp";

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
	android.util.Log.println(Log.DEBUG, LOG_TAG, "--> " + this.getClass().getName() + " onCreate().");
       setContentView(R.layout.main);

   	Uri uri = getIntent().getData();
   	if( uri != null )
   	{
   		String uriString = uri.toString();
   		android.util.Log.println(Log.DEBUG, LOG_TAG, uriString );
   	}

   }
}

2012/06/06(Wed) デッドライン直前の仕様変更は鬼過ぎる [長年日記] 編集 23:30現在 23℃

_ [Android][java]Android(Java)のアプリ作成時にOverrideでEclipseがエラー吐く場合

参考:

http://d.hatena.ne.jp/hirai428/20101111/1289465268
http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=36667&forum=12

例えば、以下のようなAndroidのコードを書いた場合に次のようなエラーが出ることがある。

「型 new DialogInterface.OnKeyListener(){} のメソッド onKey(DialogInterface, int, KeyEvent) はスーパークラスのメソッドをオーバーライドする必要があります」

 private AlertDialog.Builder builder;
 builder = new AlertDialog.Builder(this);
 builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
	if (KeyEvent.KEYCODE_BACK == keyCode && KeyEvent.ACTION_DOWN == event.getAction())		{
		return true;
	}
	return false;
}
 });

この場合、ウィンドウ→設定→Java→コンパイラーの「コンパイラー準拠レベル」を1.6以上に設定すればエラーが出なくなる。

これは、onKey()がabstractメソッドとして実装されているため。

JDKの準拠レベルが1.5の場合、既に実装されているメソッドのオーバーライドしかできないため、abstractメソッドを実装しようとするとエラーが出てしまう。

よって、abstractメソッドのオーバーライドを許容するようになったJDKの準拠レベル1.6以降に設定することで無事にコンパイルできるようになる。

以上。


2012/06/09(Sat) ガーリックトーストのうまさは異常 [長年日記] 編集 21:00現在 23℃

_ [Android][Java]AndroidでServiceを使う際のサンプル

以下に、ランチャーからアプリを起動するとServiceを自動で起動し、通知領域に情報を出すアプリのサンプルを示す。

起動したサービスはアプリのボタンを押すことでStart、Stopを切り替えることが出来る。

なお、サービスを起動したままアプリを閉じてしまったときのために、通知領域に出したメッセージをクリックすると、サービスを起動したアプリを再度呼び出すことができるようにした。

また、ブラウザからの起動用にIntentFilterを設定するため、ブラウザから以下のURLのリンクをクリックすることで、アプリを起動してサービスを実行できるようになっている。

( testapp://service/ )

【TestAppActivity.java】

package net.i.akihiro.TestApp;

import net.i.akihiro.TestApp.R.id;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class TestAppActivity extends Activity {
private static final String LOG_TAG = "TestApp";

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
	android.util.Log.println(Log.DEBUG, LOG_TAG, "--> " + this.getClass().getName() + " onCreate().");
       setContentView(R.layout.main);

   	Uri uri = getIntent().getData();
   	if( uri != null )
   	{
   		String uriString = uri.toString();
   		android.util.Log.println(Log.DEBUG, LOG_TAG, uriString );
   	}

   	//start service button
       Button button = (Button) findViewById(id.button);
       button.setOnClickListener(new View.OnClickListener() {
       	boolean isRunning = true;
           @Override
           public void onClick(View v) {
           	Button button = (Button)v;
           	Intent intent = new Intent(TestAppActivity.this, TestAppServerService.class);
           	if( isRunning == false )
           	{
               	button.setText("Stop Service");
           		startService(intent);
           	}
           	else
           	{
               	button.setText("Start Service");
           		stopService(intent);
           	}
           	isRunning = !isRunning;
           }
       });

       //startService
   	Intent intent = new Intent(TestAppActivity.this, TestAppServerService.class);
	startService(intent);
   	button.setText("Stop Service");
   }
}

【TestAppServerService.java】

package net.i.akihiro.TestApp;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class TestAppServerService extends Service {

private static final String LOG_TAG = "TestApp";

   private NotificationManager mNotification;
   int NOTIFICATION_ID = 0;

   private Intent mSelfIntent = null;

   @Override
   public IBinder onBind(Intent intent) {
       return null;
   }

   @Override
   public void onCreate() {
	Toast.makeText(this, "create service", Toast.LENGTH_SHORT).show();
	mNotification = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

       android.util.Log.println(Log.DEBUG, LOG_TAG, "create service");
   }

   @Override
   public void onStart(Intent intent, int StartId) {
   	mSelfIntent = intent;
       String message = intent.getStringExtra("Message");
       if( message != null )
       {
       	Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
       }

       try{
           // notification
           Notification notification = new Notification(android.R.drawable.btn_default, "TestApp", System.currentTimeMillis());
           notification.flags = Notification.FLAG_ONGOING_EVENT;
           // intent
           Intent targetIntent = new Intent(this, TestAppActivity.class);
           // pending intent
           PendingIntent pIntent = PendingIntent.getActivity(this, 0, targetIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
           notification.setLatestEventInfo(this, "title", "message", pIntent);
           mNotification.notify(NOTIFICATION_ID, notification);
         } catch (Exception e) {
           e.printStackTrace();
         }
   }

public void postExecute( String uniqueId )
{
	stopSelf();
	stopService(mSelfIntent);
   	mNotification.cancel(NOTIFICATION_ID);
}

   @Override
   public void onDestroy() {
   	mNotification.cancel(NOTIFICATION_ID);
   	Toast.makeText(this, "destroy service", Toast.LENGTH_SHORT).show();
       super.onDestroy();
   }
}

【main.xml】

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <TextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/hello" />

    <TextView
       android:layout_width="fill_parent"
       android:layout_height="0dp"
       android:layout_weight="1" />

   <Button android:id="@+id/button"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="StartServer" />

</LinearLayout>

【AndroidManifest.xml】

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="net.i.akihiro.TestApp"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk android:minSdkVersion="7" />

   <uses-permission android:name="android.permission.INTERNET"></uses-permission>

   <application
       android:name=".TestAppApplication"
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name" >
       <activity android:name=".TestAppActivity" android:label="@string/app_name" >
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
           <intent-filter>
			<action android:name="android.intent.action.VIEW"/>
       		<category android:name="android.intent.category.DEFAULT"/>
       		<category android:name="android.intent.category.BROWSABLE"/>
       		<data android:scheme="testapp" android:host="service" android:pathPattern="/.*"/>
           </intent-filter>
       </activity>

	<service android:name=".TestAppServerService">
	</service>

   </application>

</manifest>