2013-07-11 3 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

스택 추적을 게시 할 수 있습니까? 인 텐트 컨스트럭터의 두 번째 매개 변수는 작업 클래스 – gunar

답변

1

requestServerDelay 작업은 서비스에 있다면, 당신은 활동을 시작 getApplicationContext().startActivity(...)을 사용할 수를 확장합니다.

전에 getApplication()에 대한 필요가 없습니다.

또는 Context 멤버를 Service 클래스 (mContext)에 유지하여 사용할 수 있습니다.

+0

07-11 11 : 28 : 35.054 : W/dalvikvm (716) : threadid = 1 : catch되지 않은 예외 (그룹 = 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. 스택 추적에서 묻는 것처럼, 매니페스트에서이 활동을 선언 했습니까? –

관련 문제