我正在尝试在intentservice中下载一个json文件,下载完成后,我希望启动一个新的活动。此活动有两项任务:
-显示带有两个选项的alertDialog:重新启动/不启动
-在onsharedpreferences中保存更新的时间戳
但我一直有一个错误:
08-26 21:31:06.162: W/ActivityManager(96): Unable to start service Intent { cmp=com.hera.ontdekdelft.RESTARTACTIVITY }: not found
当然,我做了很多研究,发现很多人都有这个问题。我想我尝试了大多数给定的解决方案,但似乎没有一个对我有效。
-
我检查了我的清单,我的服务是在应用程序中声明的,而不是在活动中声明的。
-
我试着用不同的方式来表达我的意图:完整的路径(
com.hera.ontdekdelft.RESTARTACTIVITY
等等)并且类似于:
Intent restartIntent=new Intent(DownloadService.this, RestartActivity.class);
还有一些事情,要么不起作用,要么只会让事情变得更糟。现在发生的事情是,我没有弹出窗口,所以在下次重新启动后,会使用新数据(只有时间戳没有保存,我需要检查可用的更新)。没有崩溃,但没有真正起作用。
有人知道这个问题/看到我失败的地方/有解决方案吗?
谢谢
我的服务:
> import java.io.File; import java.io.FileOutputStream; import
> java.io.IOException; import java.io.InputStream; import
> java.io.InputStreamReader; import java.net.URL;
>
> import android.app.Activity; import android.app.IntentService; import
> android.content.Intent; import android.net.Uri; import
> android.os.Environment;
>
> public class DownloadService extends IntentService {
>
> private int result = Activity.RESULT_CANCELED; public String
> strfilename="DelftVenues"; public String foldername;
>
> public DownloadService() {
> super("DownloadService"); }
>
>
> @Override protected void onHandleIntent(Intent intent) { File
> dir = new File(Environment.getExternalStorageDirectory() +
> "/testOntdekDelftMap");
> foldername = dir.toString();
>
>
>
>
> Uri data = intent.getData();
> String urlPath = intent.getStringExtra("urlpath");
> String fileName = data.getLastPathSegment();
> File output = new File(foldername,fileName);
> if (output.exists()) {
> output.delete();
> }
>
> InputStream stream = null;
> FileOutputStream fos = null;
> try {
> File filename = new File(foldername, strfilename);
> URL url = new URL(urlPath);
> stream = url.openConnection().getInputStream();
> InputStreamReader reader = new InputStreamReader(stream);
> fos = new FileOutputStream(filename);
> int next = -1;
> while ((next = reader.read()) != -1) {
> fos.write(next);
> }
> // Sucessful finished
> result = Activity.RESULT_OK;
>
> } catch (Exception e) {
> e.printStackTrace();
> } finally {
> if (stream != null) {
> try {
> stream.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> if (fos != null) {
> try {
> fos.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> }
>
> System.out.println("result: "+ result);
> Intent restartIntent=new Intent(DownloadService.this, RestartActivity.class);
> this.startService(restartIntent);
>
>
> } }
我要启动的类(restartActivity)
public class RestartActivity extends Activity{
public String UPDATE_TIMESTAMP;
AlertDialog restartDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
// XXX Auto-generated method stub
super.onCreate(savedInstanceState);
restartDialog= new AlertDialog.Builder(RestartActivity.this).create();
String title, text, button1,button2;
String language = Locale.getDefault().getISO3Language();
if (language.equals("nld")){
title= "Herstarten?";
text= "De update is voltooid. Wilt u de applicatie nu opnieuw opstarten?";
button1="Ja";
button2="Nee";
}else{
title="Restart?";
text= "The application has been updated. Do you wish to restart now?";
button1="Yes";
button2="No";
}
restartDialog.setTitle(title);
restartDialog.setMessage(text);
restartDialog.setButton(button1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// restart applicatino
Intent intent = new Intent(RestartActivity.this, StartUp.class);
// Create a new Messenger for the communication back
startService(intent);
}
});
restartDialog.setButton2(button2, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
return;
}
});
restartDialog.show();
int timestampNow=(int) (System.currentTimeMillis()/1000);
setLastUpdateTimeStamp(timestampNow);
}
// timestamp opslaan
public void setLastUpdateTimeStamp(int timestamp){
SharedPreferences savedTimeStamp = getSharedPreferences(UPDATE_TIMESTAMP, MODE_PRIVATE);
SharedPreferences.Editor editor = savedTimeStamp.edit();
editor.putInt("timestamp", timestamp);
// Commit the edits!
editor.commit();
}
}
我的一份清单:
// lot more activities
<activity
android:name=".RestartActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.hera.ontdekdelft.RESTARTACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service android:name=".DownloadService" >
</service>
</application>
</manifest>