Advertisement
Guest User

Main activity reproducing Android bug

a guest
Mar 23rd, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. package com.example.test;
  2.  
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.content.ComponentName;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.ServiceConnection;
  8. import android.os.Bundle;
  9. import android.os.IBinder;
  10. import android.util.Log;
  11. import android.view.Menu;
  12. import android.view.MenuItem;
  13.  
  14. public class MainActivity extends ActionBarActivity
  15.         implements
  16.             ServiceConnection
  17. {
  18.  
  19.     private final static String TAG = "MainActivity";
  20.  
  21.     private boolean mIsServiceBound;
  22.  
  23.     @Override
  24.     protected void onCreate (Bundle savedInstanceState)
  25.     {
  26.         super.onCreate(savedInstanceState);
  27.         setContentView(R.layout.activity_main);
  28.     }
  29.  
  30.     @Override
  31.     protected void onStart ()
  32.     {
  33.         super.onStart();
  34.  
  35.         Log.v(TAG, "onStart()");
  36.  
  37.         doBindService();
  38.     }
  39.  
  40.     @Override
  41.     protected void onStop ()
  42.     {
  43.         super.onStop();
  44.  
  45.         Log.v(TAG, "onStop()");
  46.  
  47.         doUnbindService();
  48.     }
  49.  
  50.     @Override
  51.     public boolean onCreateOptionsMenu (Menu menu)
  52.     {
  53.         // Inflate the menu; this adds items to the action bar if it is present.
  54.         getMenuInflater().inflate(R.menu.main, menu);
  55.         return true;
  56.     }
  57.  
  58.     @Override
  59.     public boolean onOptionsItemSelected (MenuItem item)
  60.     {
  61.         // Handle action bar item clicks here. The action bar will
  62.         // automatically handle clicks on the Home/Up button, so long
  63.         // as you specify a parent activity in AndroidManifest.xml.
  64.         int id = item.getItemId();
  65.         if (id == R.id.action_settings)
  66.         {
  67.             return true;
  68.         }
  69.         return super.onOptionsItemSelected(item);
  70.     }
  71.  
  72.     private void doBindService ()
  73.     {
  74.         // no need to bind if already bound
  75.         if (!mIsServiceBound)
  76.         {
  77.             Log.v(TAG, "doBindService()");
  78.            
  79.             Intent intent = new Intent(this, MediaService.class);
  80.             //intent.setComponent(mServiceName);
  81.  
  82.             // start Service
  83.             startService(intent);
  84.  
  85.             // connect to service
  86.             if (14 > android.os.Build.VERSION.SDK_INT)
  87.             {
  88.                 mIsServiceBound = bindService(intent, MainActivity.this,
  89.                         Context.BIND_AUTO_CREATE);
  90.             }
  91.             else
  92.             {
  93.                 // Context.BIND_IMPORTANT = 0x40
  94.                 mIsServiceBound = bindService(intent, MainActivity.this,
  95.                         Context.BIND_AUTO_CREATE | 0x40);
  96.             }
  97.         }
  98.     }
  99.  
  100.     private void doUnbindService ()
  101.     {
  102.         if (mIsServiceBound)
  103.         {
  104.             Log.v(TAG, "doUnbindService()");
  105.  
  106.             // unbind the service!
  107.             try
  108.             {
  109.                 unbindService(MainActivity.this);
  110.             }
  111.             catch (IllegalArgumentException e)
  112.             {
  113.                 Log.e(TAG, "doUnbindService()", e);
  114.             }
  115.  
  116.             mIsServiceBound = false;
  117.         }
  118.     }
  119.  
  120.     @Override
  121.     public void onServiceConnected (ComponentName name, IBinder service)
  122.     {
  123.         Log.v(TAG, "onServiceConnected()");
  124.     }
  125.  
  126.     @Override
  127.     public void onServiceDisconnected (ComponentName name)
  128.     {
  129.         Log.w(TAG, "onServiceDisconnected()");
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement