package com.exercise.AndroidAlarmService;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
public class AlarmDialogActivity extends Activity {
static Context who;
static int screenWidth;
static int screenHeight;
static Thread tr;
private DrawView dr;
public Handler updateHandler = new Handler() {
/** Gets called on every message that is received */
// @Override
public void handleMessage(Message msg) {
if (dr != null) {
Log.w("where", "Update called");
dr.invalidate();
}
super.handleMessage(msg);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics metrics = getResources().getDisplayMetrics();
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
Log.w("haha", "haha " + screenWidth + " " + screenWidth * 0.2 + " "
+ screenWidth * 0.4 + " " + screenWidth * 0.6 + " "
+ screenWidth * 0.8 + " " + screenWidth);
dr = new DrawView(this);
setContentView(dr);
who = this;
final TextView tym = (TextView) findViewById(R.id.idalarmtime);
final LinearLayout ll = (LinearLayout) this.findViewById(R.id.layc);
tr = new Thread(new UpdateThread());
tr.start();
}
public class UpdateThread implements Runnable {
@Override
public void run() {
while (true) {
AlarmDialogActivity.this.updateHandler.sendEmptyMessage(0);
}
}
}
public void snooze() {
if (AlarmDialogActivity.this == null) {
Log.w("where", "its null");
} else {
Log.w("where", "its not null");
}
if (MyAlarmService.class == null) {
Log.w("where", "its null");
} else {
Log.w("where", "its not null");
}
Intent myIntent = new Intent(AlarmDialogActivity.this,
MyAlarmService.class);
myIntent.putExtra("id", "0");
myIntent.putExtra("tym", "21:55");
PendingIntent pendingIntent = PendingIntent.getService(
AlarmDialogActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.clear();
calendar.set(2013, 4, 21, 18, 6);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
Log.w("where", "snooze time set");
this.finish();
}
}
これがインテントサービスです
package com.exercise.AndroidAlarmService;
import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MyAlarmService extends IntentService {
public MyAlarmService() {
super("MyAlarmService");
// TODO Auto-generated constructor stub
Log.w("where", "intent Created");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.w("Where", "Gone Off");
String id = intent.getExtras().getString("id");
Log.w("Where", "" + id);
String tym = intent.getExtras().getString("tym");
Log.w("Where", "" + tym);
Service myService;
myService = this;
Intent dialogIntent = new Intent(myService, AlarmDialogActivity.class);
Bundle b = new Bundle();
b.putString("tym","21:50");
dialogIntent.putExtras(b);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
myService.startActivity(dialogIntent);
}
}
ボタンのクリックで snooze() が呼び出されます...しかし、IntentService の作成時に snooze() で nullpointerexpetion を取得しています
05-21 18:30:39.625: E/AndroidRuntime(11580): FATAL EXCEPTION: main
05-21 18:30:39.625: E/AndroidRuntime(11580): java.lang.NullPointerException
05-21 18:30:39.625: E/AndroidRuntime(11580): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
05-21 18:30:39.625: E/AndroidRuntime(11580): at android.content.ComponentName.<init>(ComponentName.java:75)
05-21 18:30:39.625: E/AndroidRuntime(11580): at android.content.Intent.<init>(Intent.java:2823)
05-21 18:30:39.625: E/AndroidRuntime(11580): at com.exercise.AndroidAlarmService.AlarmDialogActivity.snooze(AlarmDialogActivity.java:88)
05-21 18:30:39.625: E/AndroidRuntime(11580): at com.exercise.AndroidAlarmService.DrawView.onTouchEvent(DrawView.java:186)
05-21 18:30:39.625: E/AndroidRuntime(11580): at android.view.View.dispatchTouchEvent(View.java:3885)
05-21 18:30:39.625: E/AndroidRuntime(11580): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-21 18:30:39.625: E/AndroidRuntime(11580): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:942)
05-21 18:30:39.625: E/AndroidRuntime(11580): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1769)
05-21 18:30:39.625: E/AndroidRuntime(11580): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1135)
05-21 18:30:39.625: E/AndroidRuntime(11580): at android.app.Activity.dispatchTouchEvent(Activity.java:2096)
05-21 18:30:39.625: E/AndroidRuntime(11580): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1753)
05-21 18:30:39.625: E/AndroidRuntime(11580): at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2227)
05-21 18:30:39.625: E/AndroidRuntime(11580): at android.view.ViewRoot.handleMessage(ViewRoot.java:1886)
05-21 18:30:39.625: E/AndroidRuntime(11580): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 18:30:39.625: E/AndroidRuntime(11580): at android.os.Looper.loop(Looper.java:130)
05-21 18:30:39.625: E/AndroidRuntime(11580): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-21 18:30:39.625: E/AndroidRuntime(11580): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 18:30:39.625: E/AndroidRuntime(11580): at java.lang.reflect.Method.invoke(Method.java:507)
05-21 18:30:39.625: E/AndroidRuntime(11580): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-21 18:30:39.625: E/AndroidRuntime(11580): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-21 18:30:39.625: E/AndroidRuntime(11580): at dalvik.system.NativeStart.main(Native Method)