我正在尝试在 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>