gpt4 book ai didi

java - 将应用程序从服务返回到前台

转载 作者:行者123 更新时间:2023-12-01 14:20:45 26 4
gpt4 key购买 nike

Android 中相当新的内容。

这里的问题很简单。我的应用程序需要在一段时间内被唤醒。问题是我无法正确调用它。

在评论中//这里是问题代码正在破坏,给我错误,无法找到上下文并声明它。

公共(public)类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);
}

}

}

最佳答案

如果requestServerDelay任务在服务中,您可以使用getApplicationContext().startActivity(...)来启动 Activity 。

之前不需要getApplication()

或者,您可以在 Service 类 (mContext) 中保留 Context 成员并使用它。

关于java - 将应用程序从服务返回到前台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17588349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com