Reputation: 2409
i am trying to download a json file in a intentservice and when the downloading is done, i want a new activity to start. This activity has two tasks: - show an alertDialog with two options: restart/not - save an timestamp of the update in onsharedpreferences
But i have an error all the time:
08-26 21:31:06.162: W/ActivityManager(96): Unable to start service Intent { cmp=com.hera.ontdekdelft.RESTARTACTIVITY }: not found
Ofcourse i did a lot of research, and found out a lot of people had this problem. I think i tried most of the given solutions, but none of them seems to work for me.
com.hera.ontdekdelft.RESTARTACTIVITY
etc) and like this: Intent restartIntent=new Intent(DownloadService.this, RestartActivity.class);
Some more things, not working or only making it worse. What happens now is that i don't have the popup, so after the next restart the new data is used (only the timestamp isn't saved, which i need to check for available updates). No crash, but not really working as well.
Anyone knows this problem/sees where i'm failing/has a solution? Thanks!
My service:
> 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);
>
>
> } }
the class i want to start (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();
}
}
a piece of my manifest:
// 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>
Upvotes: 0
Views: 1400
Reputation: 2993
With this:
Intent restartIntent=new Intent(DownloadService.this, RestartActivity.class);
this.startService(restartIntent);
You are actually saying "start a RestartActivity as a service". Which is not what you want. This explains the error 'service Intent ... not found`.
Edit: See this question for how to start an activity from a service. And you'll run into something comparable in the button1
callback. You can use startActivity(intent)
there.
Also some off topic tips: consider giving your ui items (ie buttons) a sensible name and why not use the power of strings.xml and Android's built in localization?
Upvotes: 2