intentservice启动新意图失败:未找到

时间:2012-08-26 19:49:52

标签: android android-intent android-manifest intentservice

我正在尝试在intentservice中下载一个json文件,当下载完成后,我想要一个新的活动开始。此活动有两个任务: - 显示一个alertDialog,有两个选项:restart / not - 在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>

1 个答案:

答案 0 :(得分:2)

有了这个:

Intent restartIntent=new Intent(DownloadService.this, RestartActivity.class);
this.startService(restartIntent);

您实际上在说&#34;将RestartActivity作为服务启动&#34;。这不是你想要的。这解释了错误&#39; 服务意图...未找到。

编辑:有关如何从服务启动活动,请参阅此question。而且你会在button1回调中遇到类似的东西。您可以在那里使用startActivity(intent)

还有一些非主题提示:考虑给你的ui项目(即按钮)一个合理的名称,为什么不使用strings.xml和Android内置本地化的强大功能?