2016-08-14 3 views
1

안녕하세요, 제 튜토리얼을 온라인으로 만든 다음 내 필요에 맞게 코드를 편집하면 자동으로 업데이트됩니다. 그러나 내 logcat에서이 오류가 발생합니다.왜 java.lang.RuntimeException을 얻을 수 있습니까? ComponentInfo를 시작할 수 없습니다. ComponentInfo

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.harrops.h20droidapp/com.example.harrops.h20droidapp.Homescreen}: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.harrops.h20droidapp/com.example.harrops.h20droidapp.UpdateService}; have you declared this activity in your AndroidManifest.xml? 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 

은 어떤 하나는이 내 대화 상자 클래스

입니다이 내 서비스 클래스

public class UpdateService extends Service { 
public UpdateService() { 

} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    RequestQueue queue = Volley.newRequestQueue(getApplicationContext()); 
    String url = "<MY Link for version>"; 
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 

      if (response != null) { 
       boolean resp = response.contains("<div class='post-body entry-content' id='post-body-6791062644900393367' itemprop='description articleBody'>\n" + 
         "1.1.8\n" + 
         "<div style='clear: both;'></div>"); 
       if (!resp) { 
        //Dialog to show update 
        Intent intent1 = new Intent(UpdateService.this, UpdateDialog.class); 
        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        startActivity(intent1); 

       } else { 
        Toast.makeText(UpdateService.this, "No New Update Found..", Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      error.printStackTrace(); 
     } 
    }); 
    queue.add(stringRequest); 

    return Service.START_NOT_STICKY; 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
} 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO: Return the communication channel to the service. 
    throw new UnsupportedOperationException("Not yet implemented"); 
} 
} 

인 나에게

도와주세요 수 있습니다

public static Button btn; 
public static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Update"; 
public static File Dir = new File(path); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    AndroidAuthSession session = buildSession(); 
    dropboxAPI = new DropboxAPI<AndroidAuthSession>(session); 

    Dir.mkdir(); 

    final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 
    alertDialog.setIcon(R.mipmap.ic_launcher); 
    alertDialog.setTitle("update"); 
    alertDialog.setMessage("New update Available..."); 
    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "UPDATE", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      DownloadFromDropboxFromPath(path + "downloadFileFromDropbox", "Update/Myupdate.apk"); 


     } 
    }); 
    alertDialog.show(); 
} 




static DropboxAPI<AndroidAuthSession> dropboxAPI; 
private static final String APP_KEY = "********* **** ** **"; 
private static final String APP_SECRET = "XXX XX X X XX X"; 
private static final String ACCESSTOKEN = "xx x x xxx x x x x"; 
private DropboxAPI.UploadRequest request; 

private AndroidAuthSession buildSession() { 
    AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET); 
    AndroidAuthSession session = new AndroidAuthSession(appKeyPair); 
    session.setOAuth2AccessToken(ACCESSTOKEN); 
    return session; 
} 

static final int UploadFromSelectApp = 9501; 
static final int UploadFromFilemanager = 9502; 
public static String DropboxUploadPathFrom = ""; 
public static String DropboxUploadName = ""; 
public static String DropboxDownloadPathFrom = ""; 
public static String DropboxDownloadPathTo = ""; 

private void UploadToDropboxFromPath(String uploadPathFrom, String uploadPathTo) { 
    Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show(); 
    final String uploadPathF = uploadPathFrom; 
    final String uploadPathT = uploadPathTo; 
    Thread th = new Thread(new Runnable() { 
     public void run() { 
      File tmpFile = null; 
      try { 
       tmpFile = new File(uploadPathF); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      FileInputStream fis = null; 
      try { 
       fis = new FileInputStream(tmpFile); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 
      try { 
       dropboxAPI.putFileOverwrite(uploadPathT, fis, tmpFile.length(), null); 
      } catch (Exception e) { 
      } 
      getMain().runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show(); 
       } 
      }); 
     } 
    }); 
    th.start(); 
} 

private void UploadToDropboxFromSelectedApp(String uploadName) { 
    DropboxUploadName = uploadName; 
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("*/*"); 
    startActivityForResult(Intent.createChooser(intent, "Upload from ..."), UploadFromSelectApp); 
} 

private void UploadToDropboxFromFilemanager(String uploadName) { 
    DropboxUploadName = uploadName; 
    Intent intent = new Intent("com.sec.android.app.myfiles.PICK_DATA"); 
    intent.putExtra("CONTENT_TYPE", "*/*"); 
    intent.addCategory(Intent.CATEGORY_DEFAULT); 
    startActivityForResult(intent, UploadFromFilemanager); 
} 

private void DownloadFromDropboxFromPath(String downloadPathTo, String downloadPathFrom) { 
    DropboxDownloadPathTo = downloadPathTo; 
    DropboxDownloadPathFrom = downloadPathFrom; 
    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      Toast.makeText(getApplicationContext(), "Download file ...", Toast.LENGTH_SHORT).show(); 
      Thread th = new Thread(new Runnable() { 
       public void run() { 
        File file = new File(DropboxDownloadPathTo + DropboxDownloadPathFrom.substring(DropboxDownloadPathFrom.lastIndexOf('.'))); 
        if (file.exists()) file.delete(); 
        try { 
         FileOutputStream outputStream = new FileOutputStream(file); 
         UpdateDialog.dropboxAPI.getFile(DropboxDownloadPathFrom, null, outputStream, null); 
         getMain().runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 
           Toast.makeText(getApplicationContext(), "File successfully downloaded.", Toast.LENGTH_SHORT).show(); 
          } 
         }); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
      th.start(); 
     } 
    }); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    if (requestCode == UploadFromFilemanager) { 
     final Uri currFileURI = intent.getData(); 
     final String pathFrom = currFileURI.getPath(); 
     Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show(); 
     Thread th = new Thread(new Runnable() { 
      public void run() { 
       getMain().runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         UploadToDropboxFromPath(pathFrom, "/db-test/" + DropboxUploadName + pathFrom.substring(pathFrom.lastIndexOf('.'))); 
         Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show(); 
        } 
       }); 
      } 
     }); 
     th.start(); 
    } 
    if (requestCode == UploadFromSelectApp) { 
     Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show(); 
     final Uri uri = intent.getData(); 

     DropboxUploadPathFrom = getPath(getApplicationContext(), uri); 
     if (DropboxUploadPathFrom == null) { 
      DropboxUploadPathFrom = uri.getPath(); 
     } 
     Thread th = new Thread(new Runnable() { 
      public void run() { 
       try { 
        final File file = new File(DropboxUploadPathFrom); 
        InputStream inputStream = getContentResolver().openInputStream(uri); 

        dropboxAPI.putFile("/db-test/" + DropboxUploadName + file.getName().substring(file.getName().lastIndexOf("."), 
          file.getName().length()), inputStream, file.length(), null, new ProgressListener() { 
         @Override 
         public long progressInterval() { 
          return 100; 
         } 

         @Override 
         public void onProgress(long arg0, long arg1) { 
         } 
        }); 
        getMain().runOnUiThread(new Runnable() { 
         @Override 
         public void run() { 
          Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show(); 
         } 
        }); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
     th.start(); 
    } 
    super.onActivityResult(requestCode, resultCode, intent); 
} 

public String getPath(Context context, Uri contentUri) { 
    Cursor cursor = null; 
    try { 
     String[] proj = {MediaStore.Images.Media.DATA, MediaStore.Video.Media.DATA, MediaStore.Audio.Media.DATA}; 
     cursor = context.getContentResolver().query(contentUri, proj, null, null, null); 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     String s = cursor.getString(column_index); 
     if (s != null) { 
      cursor.close(); 
      return s; 
     } 
    } catch (Exception e) { 
    } 
    try { 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA); 
     cursor.moveToFirst(); 
     String s = cursor.getString(column_index); 
     if (s != null) { 
      cursor.close(); 
      return s; 
     } 
    } catch (Exception e) { 
    } 
    try { 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA); 
     cursor.moveToFirst(); 
     String s = cursor.getString(column_index); 
     cursor.close(); 
     return s; 
    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 
} 

public UpdateDialog getMain() { 
    return this; 
} 

} 그런 다음 마침내 홈 화면 클래스로 호출합니다. 의도

 Intent intent = new Intent(Homescreen.this, UpdateService.class); 
     startActivity(intent); 

답변

3

UpdateService으로 Service입니다. Activity이 아닙니다. 서비스를 시작하려면 startActivity()이 아니라 startService()으로 전화하십시오.

+0

감사합니다 @ CommonWare –

0

UpdateService는 활동이 아니며 서비스를 확장하는 클래스입니다.

매우 초보자라고 생각합니다. Anddroid와 함께하기 전에 Udacity로 가서 개념을 배우십시오. 그렇지 않으면 복사 붙여 넣기 솔루션을 찾는 데 많은 시간을 낭비합니다.

+0

그래서 한 줄의 코드가 잘못되었습니다. 초심자입니까? 하하하 당신은 심지어 단어를 ... 철자를 기울이지 않습니다 도움을 주셔서 감사합니다 –

+0

: | 당신의 자존심에 그것을 만들지 마라. – codepeaker

1

UpdateService은 위에서 언급 한 @CommonsWare와 같은 startService() 방법으로 서비스를 시작해야합니다. 또한 Manifest 파일에 서비스를 추가하는 것을 잊지 마십시오.

관련 문제