今天在做service小练习的时候,发现了一个问题,android有时候报错, 并不往控制台打,所以以后要养成Log的习惯。
其中URL一定要正确。否则会报Protocol not found :www.XXXXX.com
下面贴上代码,让大家一起学习。做的小练习是service后台模拟异步下载东西。
MainActivity.java
package com.example.services; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void start(View v){ Intent intent = new Intent(this,MyService.class); startService(intent); } public void stop(View v){ Intent intent = new Intent(this,MyService.class); stopService(intent); } public void down(View v){ start(v); } }
MyService.java
package com.example.services; import java.net.MalformedURLException; import java.net.URL; import android.app.Service; import android.content.Intent; import android.os.AsyncTask; import android.os.IBinder; import android.util.Log; import android.widget.Toast; public class MyService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(getBaseContext(), "onStartCommand", 1).show();; try { new DoBackgroundTask().execute( new URL("www.12345.com"), new URL("www.12345.com"), new URL("www.12345.com"), new URL("www.12345.com")); } catch (MalformedURLException e) { Log.i("app", e.getMessage()); e.printStackTrace(); } return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(this, "onDestroy", 1).show(); } private int downloadFile(URL url){ try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } return 100; } // private class DoBackgroundTask extends AsyncTask{ // // @Override // protected Long doInBackground(URL... params) { // Log.i("app", "doInBackground"); // int count = params.length; // long totalBytesDownload = 0; // for (int i = 0; i < count; i++) { // Log.i("app", "i="+i); // totalBytesDownload += downloadFile(params[i]); // publishProgress(((i+1)/count)*100); // } // return totalBytesDownload; // } // @Override // protected void onProgressUpdate(Integer... values) { // Log.i("app", "onProgressUpdate"); // Toast.makeText(getBaseContext(),"下载"+values[0]+"%", 1).show(); // super.onProgressUpdate(values); // } // @Override // protected void onPostExecute(Long result) { // Toast.makeText(getBaseContext(),"下载"+result+"kb", 1).show(); // super.onPostExecute(result); // } // } private class DoBackgroundTask extends AsyncTask{ protected Long doInBackground(URL... urls) { int count = urls.length; long totalBytesDownloaded = 0; for (int i = 0; i < count; i++) { totalBytesDownloaded += downloadFile(urls[i]); //---calculate percentage downloaded and // report its progress--- publishProgress((int) (((i+1) / (float) count) * 100)); } return totalBytesDownloaded; } protected void onProgressUpdate(Integer... progress) { Log.d("Downloading files", String.valueOf(progress[0]) + "% downloaded"); Toast.makeText(getBaseContext(), String.valueOf(progress[0]) + "% downloaded", Toast.LENGTH_LONG).show(); } protected void onPostExecute(Long result) { Toast.makeText(getBaseContext(), "Downloaded " + result + " bytes", Toast.LENGTH_LONG).show(); stopSelf(); } } }
AndroidManifest.xml
xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.services" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> intent-filter> activity> <service android:name=".MyService" >service> application> manifest>