这是我的代码,时间从首选项和设置后得到一个正确的calander格式,然后执行timertask设置notificatin
public class MyService extends Service {
private static final String TAG = "com.wat.compassapp";
private SharedPreferences myPrefs;
int mYear, mMonth, mDay, msecs;
String fadjrtime, dhuhrtime, asrtime, magrhibtime, ishatime, citygmt;
private NotificationManager mManager;
private Context context;
MediaPlayer mMediaPlayer;
Time sqlTime3, fajrsqlTime3, dhuhrsqlTime3, asrsqlTime3, maghribsqlTime3,
ishasqlTime3;
static long miliSeconds;
static Date resultdate;
Dialog dialog;
AlertDialog.Builder builder;
Timer mTimer = new Timer();
TimerTask mDataTask;
Calendar c;
AlertDialog dail;
@Override
public void onCreate() {
Log.i(TAG, "Service onCreate");
myPrefs = getSharedPreferences("myPrefs", MODE_PRIVATE);
mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
fadjrtime = myPrefs.getString("fadjrtime", null);
dhuhrtime = myPrefs.getString("dhuhrtime", null);
asrtime = myPrefs.getString("asrtime", null);
magrhibtime = myPrefs.getString("magrhibtime", null);
ishatime = myPrefs.getString("ishatime", null);
mMediaPlayer = new MediaPlayer();
citygmt = myPrefs.getString("citygmt", null);
dialog = new Dialog(getApplicationContext());
// dialog.setContentView(R.layout.)
builder = new AlertDialog.Builder(getApplicationContext())
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(getApplicationContext(), "OK",
1000).show();
}
})
.setNegativeButton(android.R.string.no,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// do nothing
}
}).setIcon(android.R.drawable.ic_dialog_alert);
dail = builder.create();
dail.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
dail.show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d(TAG, "onStart");
final Calendar ca = Calendar.getInstance();
String[] sca = fadjrtime.split(":");
ca.set(Calendar.HOUR_OF_DAY, Integer.parseInt(sca[0]));
ca.set(Calendar.MINUTE, Integer.parseInt(sca[1]));
ca.set(Calendar.SECOND, 00);
SimpleDateFormat ra = new SimpleDateFormat("HH:mm:ss");
String fayourTime = Integer.parseInt(sca[0]) + ":"
+ Integer.parseInt(sca[1]) + ":00";
Date fayourDate;
try {
fayourDate = ra.parse(fayourTime);
fajrsqlTime3 = new Time(fayourDate.getTime());
Log.d("fajrsqlTime3 on prayerfragment", fajrsqlTime3 + "");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final Calendar cal = Calendar.getInstance();
String[] scal = dhuhrtime.split(":");
cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(scal[0]));
cal.set(Calendar.MINUTE, Integer.parseInt(scal[1]));
cal.set(Calendar.SECOND, 00);
String dhyourTime = Integer.parseInt(scal[0]) + ":"
+ Integer.parseInt(scal[1]) + ":00";
Date dhyourDate;
try {
dhyourDate = ra.parse(dhyourTime);
dhuhrsqlTime3 = new Time(dhyourDate.getTime());
Log.d("dhuhrsqlTime3 on prayerfragment", dhuhrsqlTime3 + "");
} catch (ParseException e) {
e.printStackTrace();
}
final Calendar cale = Calendar.getInstance();
String[] scale = asrtime.split(":");
cale.set(Calendar.HOUR_OF_DAY, Integer.parseInt(scale[0]));
cale.set(Calendar.MINUTE, Integer.parseInt(scale[1]));
cale.set(Calendar.SECOND, 00);
String asyourTime = Integer.parseInt(scale[0]) + ":"
+ Integer.parseInt(scale[1]) + ":00";
Date asyourDate;
try {
asyourDate = ra.parse(asyourTime);
asrsqlTime3 = new Time(asyourDate.getTime());
Log.d("asrsqlTime3 on prayerfragment", asrsqlTime3 + "");
} catch (ParseException e) {
e.printStackTrace();
}
final Calendar calen = Calendar.getInstance();
String[] scalen = magrhibtime.split(":");
calen.set(Calendar.HOUR_OF_DAY, Integer.parseInt(scalen[0]));
calen.set(Calendar.MINUTE, Integer.parseInt(scalen[1]));
calen.set(Calendar.SECOND, 00);
String mayourTime = Integer.parseInt(scalen[0]) + ":"
+ Integer.parseInt(scalen[1]) + ":00";
Date mayourDate;
try {
mayourDate = ra.parse(mayourTime);
maghribsqlTime3 = new Time(mayourDate.getTime());
Log.d("maghribsqlTime3 on prayerfragment", maghribsqlTime3 + "");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
final Calendar calend = Calendar.getInstance();
String[] scalend = ishatime.split(":");
calend.set(Calendar.HOUR_OF_DAY, Integer.parseInt(scalend[0]));
calend.set(Calendar.MINUTE, Integer.parseInt(scalend[1]));
calend.set(Calendar.SECOND, 00);
String isyourTime = Integer.parseInt(scalend[0]) + ":"
+ Integer.parseInt(scalend[1]) + ":00";
Date isyourDate;
try {
isyourDate = ra.parse(isyourTime);
ishasqlTime3 = new Time(isyourDate.getTime());
Log.d("ishasqlTime3 on prayerfragment", ishasqlTime3 + "");
} catch (ParseException e) {
e.printStackTrace();
}
Log.i(TAG, "Service running");
// Log.i("Service running sqlTime3", sqlTime3 + "");
mDataTask = new MyTimerTask();
mTimer.scheduleAtFixedRate(mDataTask, 0, 1000 /* 60000 */);
return START_STICKY;
}
@Override
public void onStart(Intent intent, int startId) {
}
private class MyTimerTask extends TimerTask {
@Override
public void run() {
if (citygmt != null) {
if (citygmt.length() > 0) {
Calendar c = Calendar.getInstance(TimeZone
.getTimeZone("GMT"));
Log.d("citygmt dsfs", citygmt);
c.setTimeZone(TimeZone.getTimeZone(citygmt));
System.out.println("The Date in the local time zone "
+ c.get(Calendar.HOUR_OF_DAY) + ":"
+ c.get(Calendar.MINUTE) + ":"
+ c.get(Calendar.SECOND));
String yourTime = c.get(Calendar.HOUR_OF_DAY) + ":"
+ c.get(Calendar.MINUTE) + ":"
+ c.get(Calendar.SECOND);
// msecs = c.get(Calendar.SECOND);
SimpleDateFormat ra = new SimpleDateFormat("HH:mm:ss");
Date yourDate;
try {
yourDate = ra.parse(yourTime);
sqlTime3 = new Time(yourDate.getTime());
Log.d("sqlTime3 on prayerfragment 1", sqlTime3 + "");
} catch (ParseException e) {
e.printStackTrace();
}
SharedPreferences.Editor editor = myPrefs.edit();
editor.putString("citygmt", "");
editor.commit();
} else {
Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
msecs = c.get(Calendar.SECOND);
String yourTime = c.get(Calendar.HOUR_OF_DAY) + ":"
+ c.get(Calendar.MINUTE) + ":"
+ c.get(Calendar.SECOND);
msecs = c.get(Calendar.SECOND);
SimpleDateFormat ra = new SimpleDateFormat("HH:mm:ss");
Date yourDate;
try {
yourDate = ra.parse(yourTime);
sqlTime3 = new Time(yourDate.getTime());
Log.d("sqlTime3 on prayerfragment 2", sqlTime3 + "");
} catch (ParseException e) {
e.printStackTrace();
}
}
} else {
Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
msecs = c.get(Calendar.SECOND);
String yourTime = c.get(Calendar.HOUR_OF_DAY) + ":"
+ c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND);
msecs = c.get(Calendar.SECOND);
SimpleDateFormat ra = new SimpleDateFormat("HH:mm:ss");
Date yourDate;
try {
yourDate = ra.parse(yourTime);
sqlTime3 = new Time(yourDate.getTime());
Log.d("Service sqlTime3 calenadr",
c.get(Calendar.HOUR_OF_DAY) + ":"
+ c.get(Calendar.MINUTE) + ":"
+ c.get(Calendar.SECOND) + "");
Log.d("sqlTime3 on prayerfragment 3", sqlTime3 + "");
} catch (ParseException e) {
e.printStackTrace();
}
}
Log.d("Service sqlTime3", sqlTime3 + "");
Log.d("Service fajrsqlTime3", fajrsqlTime3 + "");
Log.d("Service dhuhrsqlTime3", dhuhrsqlTime3 + "");
Log.d("Service asrsqlTime3", asrsqlTime3 + "");
Log.d("Service maghribsqlTime3", maghribsqlTime3 + "");
Log.d("Service ishasqlTime3", ishasqlTime3 + "");
if (sqlTime3.equals(fajrsqlTime3) && !sqlTime3.after(fajrsqlTime3)) {
// mMediaPlayer.start();
GenerateNotification(getApplicationContext(), "fajr");
// dail.show();
} else if (sqlTime3.after(fajrsqlTime3)
&& sqlTime3.equals(dhuhrsqlTime3)
&& !sqlTime3.after(dhuhrsqlTime3)) {
Log.i(TAG, "Service dhuhrsqlTime3");
// mMediaPlayer.start();
GenerateNotification(getApplicationContext(), "dhuhr");
// dail.show();
}
else if (sqlTime3.after(fajrsqlTime3)
&& sqlTime3.after(dhuhrsqlTime3)
&& sqlTime3.equals(asrsqlTime3)
&& !sqlTime3.after(asrsqlTime3)) {
Log.i(TAG, "Service asrsqlTime3");
// mMediaPlayer.start();
GenerateNotification(getApplicationContext(), "asr");
// dail.show();
}
else if (sqlTime3.after(fajrsqlTime3)
&& sqlTime3.after(dhuhrsqlTime3)
&& sqlTime3.after(asrsqlTime3)
&& sqlTime3.equals(maghribsqlTime3)
&& !sqlTime3.after(maghribsqlTime3)) {
Log.i(TAG, "Service maghribsqlTime3");
// mMediaPlayer.start();
GenerateNotification(getApplicationContext(), "maghrib");
// dail.show();
} else if (sqlTime3.equals(ishasqlTime3)
&& !sqlTime3.after(ishasqlTime3)) {
Log.i(TAG, "Service ishasqlTime3");
// mMediaPlayer.start();
GenerateNotification(getApplicationContext(), "isha");
// dail.show();
} /*
* else if (sqlTime3.after(ishasqlTime3)) { Log.i(TAG,
* "Service ishasqlTime3"); mMediaPlayer.start();
* GenerateNotification(getApplicationContext(), "fajr"); }
*/
}
}
@SuppressWarnings("deprecation")
private void GenerateNotification(Context context, String string) {
Intent intent1 = new Intent(context, Selectlanguag.class);
Notification notification = new Notification(R.drawable.islamiclogo,
" Prayer time for " + string, System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
context, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, "WAT Prayer",
" Upcoming prayer time is " + string,
pendingNotificationIntent);
Uri path = Uri.parse("android.resource://com.wat.compassapp/raw/du");
notification.sound = path;
mManager.notify(0, notification);
}
@Override
public void onDestroy() {
Log.w(TAG, "onDestroy");
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
Log.i(TAG, "Service onBind");
return null;
}
}
我想在显示通知时显示alertdialog(用于暂停通知)我使用系统窗口警报
请帮我写一下这个问题的完美代码
答案 0 :(得分:0)
那么你在调用generateNotification方法的地方,只需使用runOnUiThread,然后在这里显示你的对话框。在您的活动中,将上下文设置为static:static Contaxt context = YourActivity.this;
((Activity) YourActivity.context)
.runOnUiThread(new Runnable() {
@Override
public void run() {
// place your dialog here
}
});