Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 18th, 2012  |  syntax: None  |  size: 5.31 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Stopping Android service
  2. public class ServicedownloadActivity extends Activity implements
  3.         OnClickListener {
  4.     @Override
  5.     public void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.main);
  8.         b1 = (Button) findViewById(R.id.button1);
  9.         b2 = (Button) findViewById(R.id.button2);
  10.         b1.setOnClickListener(this);
  11.         b2.setOnClickListener(this);
  12.     }
  13.  
  14.     @Override
  15.     public void onClick(View v) {
  16.  
  17.         if (v.getId() == R.id.button1) {
  18.             Intent intent = new Intent(ServicedownloadActivity.this,
  19.                     Myclass.class);
  20.             startService(intent);
  21.  
  22.         }
  23.         if (v.getId() == R.id.button2) {
  24.  
  25.             Intent intent = new Intent(ServicedownloadActivity.this,
  26.                     Myclass.class);
  27.             stopService(intent);
  28.         }
  29.     }
  30. }
  31.        
  32. public class Myclass extends Service {
  33.  
  34.     URLConnection connection;
  35.     private final Random mGenerator = new Random();
  36.  
  37.     @Override
  38.     public IBinder onBind(Intent intent) {
  39.         return null;
  40.     }
  41.  
  42.     public int getRandomNumber() {
  43.         return mGenerator.nextInt(100);
  44.     }
  45.  
  46.     @Override
  47.     public void onCreate() {
  48.         super.onCreate();
  49.         Log.d("df", "From oncreate");
  50.         Toast.makeText(this, "on create", Toast.LENGTH_SHORT).show();
  51.  
  52.     }
  53.     class DownloadWebPageTask extends AsyncTask<String, String, String> {
  54.         String responseTextt;
  55.         NotificationManager notificationManager;
  56.         Notification notification;
  57.         CharSequence contentText;
  58.         Context context;
  59.         CharSequence contentTitle;
  60.         PendingIntent contentIntent;
  61.         int HELLO_ID = 1;
  62.         long time;
  63.         int icon;
  64.         CharSequence tickerText;
  65.  
  66.         public void downloadNotification() {
  67.             String ns = Context.NOTIFICATION_SERVICE;
  68.             notificationManager = (NotificationManager) getSystemService(ns);
  69.             icon = R.drawable.ic_launcher;
  70.             tickerText = "Downloading...";
  71.             time = System.currentTimeMillis();
  72.             notification = new Notification(icon, tickerText, time);
  73.             context = getApplicationContext();
  74.             contentTitle = "Your download is in progress";
  75.             contentText = "0% complete";
  76.             Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
  77.             notificationIntent.setType("audio/*");
  78.             contentIntent = PendingIntent.getActivity(context, 0,
  79.                     notificationIntent, 0);
  80.             notification.setLatestEventInfo(context, contentTitle,
  81.                     contentText, contentIntent);
  82.             notificationManager.notify(HELLO_ID, notification);
  83.  
  84.         }
  85.  
  86.         @Override
  87.         protected String doInBackground(String... urls) {
  88.  
  89.             try {
  90.  
  91.                 Log.d("trys:", "try");
  92.                 URL url = new URL(
  93.                         "http://www.zapiture.com/resources/IMG_6812.JPG");
  94.                 connection = url.openConnection();
  95.                 connection.connect();
  96.                 int fileLength = connection.getContentLength();
  97.                 InputStream input = new BufferedInputStream(url
  98.                         .openStream());
  99.                 OutputStream output = new FileOutputStream(Environment
  100.                         .getExternalStorageDirectory()
  101.                         + "/img23.jpg");
  102.  
  103.                 byte data[] = new byte[1024];
  104.                 long total = 0;
  105.                 int count;
  106.                 int previousProgress = 0;
  107.                 while ((count = input.read(data)) != -1) {
  108.                     total += count;
  109.                     if ((int) (total * 100 / fileLength) > previousProgress) {
  110.                         previousProgress = (int) (total * 100 / fileLength);
  111.                         publishProgress(""
  112.                                 + (int) (total * 100 / fileLength));
  113.                         output.write(data, 0, count);
  114.                     }
  115.                 }
  116.  
  117.                 output.flush();
  118.                 output.close();
  119.                 input.close();
  120.  
  121.             } catch (Exception e) {
  122.                 e.printStackTrace();
  123.             }
  124.  
  125.             return null;
  126.         }
  127.  
  128.         @Override
  129.         protected void onPreExecute() {
  130.             downloadNotification();
  131.  
  132.             super.onPreExecute();
  133.             ;
  134.         }
  135.  
  136.         @Override
  137.         public void onProgressUpdate(String... progress) {
  138.  
  139.             contentText = Integer.parseInt(progress[0]) + "% complete";
  140.             notification.setLatestEventInfo(context, contentTitle,
  141.                     contentText, contentIntent);
  142.             notificationManager.notify(HELLO_ID, notification);
  143.             super.onProgressUpdate(progress);
  144.         }
  145.     }
  146.  
  147.     @Override
  148.     public void onStart(Intent intent, int startId) {
  149.         // TODO Auto-generated method stub
  150.         super.onStart(intent, startId);
  151.         Log.d("sf", "On Start");
  152.         Toast.makeText(this, "on start", Toast.LENGTH_SHORT).show();
  153.         DownloadWebPageTask task = new DownloadWebPageTask();
  154.  
  155.         task.execute();
  156.     }
  157.  
  158.     @Override
  159.     public void onDestroy() {
  160.         stopSelf();
  161.         super.onDestroy();
  162.         Log.d("df", "From on Destroy");
  163.         Toast.makeText(this, "on destroy", Toast.LENGTH_SHORT).show();
  164.     }
  165. }
  166.        
  167. task.cancel(true);
  168.        
  169. @Override
  170. public void onDestroy() {
  171.     task.canel();
  172.     super.onDestroy();
  173. }