2013-07-11 31 views
0

Android中很新穎。將應用程序從服務中移回前臺

這裏的問題很簡單。 我的應用程序需要在某段時間內喚醒。 問題是我無法正確調用它。

有評論//這裏是問題的代碼打破給我錯誤,找不到上下文,並宣佈它。

公共類BackgroundService延伸服務{

Integer background_connect = null; 
String SERVER_IP; 
String APP_ID; 
private Context context; 

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

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

    DB_Queries db = new DB_Queries(getApplicationContext()); 
    Cursor c = db.getSettings(); 
    StringBuilder app_id = new StringBuilder(); 
    StringBuilder server_ip = new StringBuilder(); 
    if (c.moveToFirst()) { 
     do { 
      server_ip.append(c.getString(c.getColumnIndex("server_ip"))); 
      app_id.append(c.getString(c.getColumnIndex("app_id"))); 
     } while (c.moveToNext()); 
    } 
    db.close(); 

    SERVER_IP = server_ip.toString(); 
    APP_ID = app_id.toString(); 
} 

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

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

    requestServerDelay delay = new requestServerDelay(); 
    delay.execute("http://" + SERVER_IP + "/index.php/app/requestAppRecall"); 

    return super.onStartCommand(intent, flags, startId); 
} 

private class requestServerDelay extends AsyncTask<String, Void, Void> { 

    @Override 
    protected Void doInBackground(String... params) { 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost post = new HttpPost(params[0]); 
     post.addHeader("Content-type", "application/x-www-form-urlencoded"); 
     List<NameValuePair> pair = new ArrayList<NameValuePair>(); 
     pair.add(new BasicNameValuePair("app_id", APP_ID)); 

     try { 
      post.setEntity(new UrlEncodedFormEntity(pair)); 
      HttpResponse response = client.execute(post); 
      InputStream is = response.getEntity().getContent(); 
      InputStreamReader isr = new InputStreamReader(is); 
      BufferedReader br = new BufferedReader(isr); 
      StringBuilder str = new StringBuilder(); 
      String chunk = null; 

      while ((chunk = br.readLine()) != null) { 
       str.append(chunk); 
      } 

      background_connect = Integer.parseInt(str.toString()); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      public void run() { 
       //HERE IS THE PROBLEM 
       Intent intent = new Intent(getApplication().getApplicationContext(), getApplication().getApplicationContext().getClass()); 
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
        intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 
        intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP); 
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        getApplication().getApplicationContext().startActivity(intent); 
      } 
     }, background_connect * 60 * 1000); 
    } 

} 

}

+0

你可以發佈堆棧跟蹤嗎?您的Intent構造函數中的第二個參數必須指向您活動的類別 – gunar

回答

1

如果requestServerDelay任務是在服務,您可以使用getApplicationContext().startActivity(...)開始活動。

之前不需要getApplication()

或者您可以在Service類(mContext)中保留一個Context成員並使用它。

+0

07-11 11:28:35.054:W/dalvikvm(716):threadid = 1:以未捕獲的異常退出(組= 0x409c01f8) 07-11 11:28:35.064:E/AndroidRuntime(716):致命例外:主 07-11 11:28:35.064:E/AndroidRuntime(716):android.content.ActivityNotFoundException:無法找到顯式活動類{com.AndAds .v1/android.app.Application};你有沒有在你的AndroidManifest.xml中聲明這個活動? –

+0

@ ventsi.slav很好..當堆棧跟蹤詢問,你有沒有在你的清單中聲明這個活動? –

相關問題