Java 将应用程序从服务带回前台
在安卓系统中非常新 这里的问题很简单。 我的应用程序需要在一段时间内唤醒。 问题是我不能恰当地称呼它 注释//中有一个问题,代码正在破坏,给了我一个无法找到上下文并声明上下文的错误 公共类后台服务扩展服务{Java 将应用程序从服务带回前台,java,android,Java,Android,在安卓系统中非常新 这里的问题很简单。 我的应用程序需要在一段时间内唤醒。 问题是我不能恰当地称呼它 注释//中有一个问题,代码正在破坏,给了我一个无法找到上下文并声明上下文的错误 公共类后台服务扩展服务{ Integer background_connect = null; String SERVER_IP; String APP_ID; private Context context; @Override public IBinder onBind(Intent arg0) { r
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);
}
}
整数背景\u connect=null;
字符串服务器;
字符串APP_ID;
私人语境;
@凌驾
公共IBinder onBind(意图arg0){
返回null;
}
@凌驾
public void onCreate(){
super.onCreate();
DB_querys DB=新的DB_查询(getApplicationContext());
游标c=db.getSettings();
StringBuilder应用程序_id=新建StringBuilder();
StringBuilder服务器_ip=新建StringBuilder();
if(c.moveToFirst()){
做{
服务器ip.append(c.getString(c.getColumnIndex(“服务器ip”));
app_id.append(c.getString(c.getColumnIndex(“app_id”));
}而(c.moveToNext());
}
db.close();
SERVER_IP=SERVER_IP.toString();
APP_ID=APP_ID.toString();
}
@凌驾
公共空间{
super.ondestory();
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
requestServerDelay delay=新的requestServerDelay();
delay.execute(“http://“+SERVER_IP+”/index.php/app/requestAppRecall”);
返回super.onStartCommand(intent、flags、startId);
}
私有类requestServerDelay扩展异步任务{
@凌驾
受保护的Void doInBackground(字符串…参数){
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(参数[0]);
post.addHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
列表对=新的ArrayList();
添加(新的BasicNameValuePair(“应用程序id”,应用程序id));
试一试{
post.setEntity(新的UrlEncodedFormEntity(对));
HttpResponse response=client.execute(post);
InputStream is=response.getEntity().getContent();
InputStreamReader isr=新的InputStreamReader(is);
BufferedReader br=新的BufferedReader(isr);
StringBuilder str=新的StringBuilder();
字符串chunk=null;
而((chunk=br.readLine())!=null){
str.append(chunk);
}
background_connect=Integer.parseInt(str.toString());
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
Handler=newhandler();
handler.postDelayed(新的Runnable(){
公开募捐{
//问题就在这里
意向意向=新意向(getApplication().getApplicationContext(),getApplication().getApplicationContext().getClass());
intent.addFlags(intent.FLAG\u ACTIVITY\u REORDER\u TO\u FRONT);
intent.addFlags(intent.FLAG\活动\重置\任务\如果需要);
intent.addFlags(intent.FLAG\u ACTIVITY\u PREVIOUS\u为顶部);
intent.addFlags(intent.FLAG\u活动\u新任务);
getApplication().getApplicationContext().startActivity(意图);
}
},后台连接*60*1000);
}
}
}如果服务中有
requestServerDelay
任务,则可以使用getApplicationContext().startActivity(…)
启动活动
之前不需要getApplication()
或者,您可以在服务类(mContext)中保留一个上下文成员并使用它。您可以发布堆栈跟踪吗?Intent构造函数中的第二个参数必须指向活动的类07-11 11:28:35.054:W/dalvikvm(716):threadid=1:thread带未捕获异常退出(group=0x409c01f8)07-11 11:28:35.064:E/AndroidRuntime(716):致命异常:main 07-11:28:35.064:E/AndroidRuntime(716):android.content.ActivityNotFoundException:找不到显式活动类{com.AndAds.v1/android.app.Application};您是否在AndroidManifest.xml中声明了此活动?@ventsi.slav well。。正如堆栈跟踪所问,您是否在清单中声明了此活动?