我知道有很多代碼與此相關!但我仍然面臨問題 我GOOGLE了很多教程和文檔,我能夠運行服務,當我最小化應用程序!但它在應用程序關閉或終止時沒有響應當應用程序被終止,服務不能在後臺工作
這是我的代碼!這是我的服務類
public class MyService extends Service {
final public static String ONE_TIME = "onetime";
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences sharedpreferences;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "ServiceClass.onStart()", Toast.LENGTH_LONG).show();
Log.d("Testing", "Service got started");
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
//I need to call this every say-10 second
final String URL = "http://nrna.org.np/nrna_app/app_user/set_location/" + sharedpreferences.getString("Save_user_app_id", null) + "/np";
// if (isNetworkAvailable() == true) {
RequestQueue queue = Volley.newRequestQueue(MyService.this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, null, null);
// Add the request to the RequestQueue.
queue.add(stringRequest);
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}
,我從我的主要活動
// Method to start the service
public void startService(Context context) {
Intent intent = new Intent(MainActivity.this, MyService.class);
PendingIntent pintent = PendingIntent.getService(MainActivity.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10 * 1000, pintent);
//startService(new Intent(getBaseContext(), MyService.class));
}
我的清單文件
<service android:name=".MyService"/>
我需要的,如果應用程序被關閉運行調用該服務!
UPDATE我使用了BroadCastReceiver!但不能再次工作。
public class BaseNotificationManager extends BroadcastReceiver {
public static final String BaseAction = "Com.TST.BaseAction";
public static final String FireService = "Com.TST.FireNotificationAction";
private static final long timeFrame = 1000*10; // 5 mints
public BaseNotificationManager() {
}
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
/// add base alarm manager
startBaseAlarmManager(context);
}else if (BaseAction.equals(intent.getAction())){
// StartYourService();
intent = new Intent(context,MyService.class);
PendingIntent pintent = PendingIntent.getService(context, 0, intent, 0);
AlarmManager alarm = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10 * 1000, pintent);
}
} public static void startBaseAlarmManager (Context context){
AlarmManager alarmMgr;
PendingIntent alarmIntent;
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, BaseNotificationManager.class);
intent.setAction(BaseAction);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
5000, timeFrame, alarmIntent);
}}
嘗試傳遞'this'而不是'getBaseContext()' – Vucko
沒有。它不工作! –