سلام
وقت بخیر
من می خوام وقتی اپلیکیشن باز نیست یه کاری رو رو گوشی بتونم انجام بدم
برای این کار از سرویس استفاده می کنم
و در اولین اکتیویتی اپلیکیشنم فراخونیش می کنم :
تا وقتی اپلیکیشن باز هست یا مینیمایز هست مشکلی نیست و سرویس کار می کنه اما وقتی اپ بسته میشه سرویس از کار میفته
مگه سرویس ها برای این نیستند که بدون باز بودن اپلیکیشن و در بک گراند کار انجام بدن؟!
کد های سرویس من :
وقت بخیر
من می خوام وقتی اپلیکیشن باز نیست یه کاری رو رو گوشی بتونم انجام بدم
برای این کار از سرویس استفاده می کنم
و در اولین اکتیویتی اپلیکیشنم فراخونیش می کنم :
کد پیاچپی:
startService(new Intent(this, MyService.class));
تا وقتی اپلیکیشن باز هست یا مینیمایز هست مشکلی نیست و سرویس کار می کنه اما وقتی اپ بسته میشه سرویس از کار میفته
مگه سرویس ها برای این نیستند که بدون باز بودن اپلیکیشن و در بک گراند کار انجام بدن؟!
کد های سرویس من :
کد پیاچپی:
public class MyService extends Service{
private static String TAG = "Inchoo.net tutorial";
int mNotificationId = 001;
Notification n;
NotificationManager nm;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.d(TAG, "FirstService started");
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
readWebpage();
handler.postDelayed(this, 15000); //now is every 2 minutes
}
}, 15000); //Every 120000 ms (2 minutes)
this.stopSelf();
}
/*
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(TAG, "FirstService destroyed");
}*/
private void readWebpage() {
String appName = "com.fallrooz.srp";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appName));
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new Notification.Builder(this)
.setContentTitle("تست نوتیفیکیشن")
.setContentText("تست تست تست")
.setSmallIcon(R.drawable.icon)
.setContentIntent(pIntent)
.setAutoCancel(true)
.setSound(RingtoneManager.getDefaultUri(RingtoneMa nager.TYPE_NOTIFICATION))
.addAction(R.drawable.icon, "Call", pIntent)
.addAction(R.drawable.icon, "More", pIntent)
.addAction(R.drawable.icon, "And more", pIntent).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
//Toast.makeText(this, "shod", Toast.LENGTH_SHORT).show();
}
}