Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.35 KB | None | 0 0
  1. public class Main2Activity extends AppCompatActivity
  2.         implements NavigationView.OnNavigationItemSelectedListener,LocationListener {
  3.  
  4.     private LocationManager locationManager;
  5.     private Location location;
  6.  
  7.     @Override
  8.     protected void onCreate(Bundle savedInstanceState) {
  9.         super.onCreate(savedInstanceState);
  10.         setContentView(R.layout.activity_main2);
  11.  
  12.         StoreListFragment storeListFragment=new StoreListFragment();
  13.         getSupportFragmentManager().beginTransaction().replace(R.id.container,storeListFragment).commit();
  14.  
  15.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  16.         setSupportActionBar(toolbar);
  17.  
  18.         locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
  19.  
  20.         if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){
  21.  
  22.             //Cek izin
  23.             if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!=
  24.                                  PackageManager.PERMISSION_GRANTED){
  25.                 //Minta Izin
  26.                 ActivityCompat.requestPermissions(this,
  27.                         new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
  28.                 return;
  29.             }
  30.  
  31.         }
  32.  
  33.         locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10,10, (LocationListener) this);
  34.         PopupUtil.showLoading(this,"","Finding Your location ......");
  35.  
  36.  
  37.  
  38.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  39.         fab.setOnClickListener(new View.OnClickListener() {
  40.             @Override
  41.             public void onClick(View view) {
  42.                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  43.                         .setAction("Action", null).show();
  44.             }
  45.         });
  46.  
  47.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  48.         ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  49.                 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  50.         drawer.addDrawerListener(toggle);
  51.         toggle.syncState();
  52.  
  53.         NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  54.         navigationView.setNavigationItemSelectedListener(this);
  55.     }
  56.  
  57.     @Override
  58.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  59.  
  60.  
  61.         if (requestCode==1){
  62.  
  63.             if (grantResults[0]==PackageManager.PERMISSION_GRANTED){
  64.                 //Diizinkan oleh user
  65.                 //minta lokasi
  66.                 if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!=
  67.                         PackageManager.PERMISSION_GRANTED &&
  68.                         ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)!=
  69.                                 PackageManager.PERMISSION_GRANTED){
  70.                     return;
  71.                 }
  72.  
  73.                 locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10,10, (LocationListener) this);
  74.                 PopupUtil.showLoading(this,"","Finding Your location ......");
  75.             }
  76.  
  77.         }
  78.  
  79.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  80.     }
  81.    
  82.    
  83.  
  84.     @Override
  85.     public void onBackPressed() {
  86.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  87.         if (drawer.isDrawerOpen(GravityCompat.START)) {
  88.             drawer.closeDrawer(GravityCompat.START);
  89.         } else {
  90.             super.onBackPressed();
  91.         }
  92.     }
  93.  
  94.     @Override
  95.     public boolean onCreateOptionsMenu(Menu menu) {
  96.         // Inflate the menu; this adds items to the action bar if it is present.
  97.         getMenuInflater().inflate(R.menu.main2, menu);
  98.         return true;
  99.     }
  100.  
  101.     @Override
  102.     public boolean onOptionsItemSelected(MenuItem item) {
  103.         // Handle action bar item clicks here. The action bar will
  104.         // automatically handle clicks on the Home/Up button, so long
  105.         // as you specify a parent activity in AndroidManifest.xml.
  106.         int id = item.getItemId();
  107.  
  108.         //noinspection SimplifiableIfStatement
  109.         if (id == R.id.action_settings) {
  110.             return true;
  111.         }
  112.  
  113.         return super.onOptionsItemSelected(item);
  114.     }
  115.  
  116.     @SuppressWarnings("StatementWithEmptyBody")
  117.     @Override
  118.     public boolean onNavigationItemSelected(MenuItem item) {
  119.         // Handle navigation view item clicks here.
  120.         int id = item.getItemId();
  121.  
  122.         if (id == R.id.nav_camera) {
  123.             // Handle the camera action
  124.         } else if (id == R.id.nav_gallery) {
  125.  
  126.         } else if (id == R.id.nav_slideshow) {
  127.  
  128.         } else if (id == R.id.nav_manage) {
  129.  
  130.         } else if (id == R.id.nav_share) {
  131.  
  132.         } else if (id == R.id.nav_send) {
  133.  
  134.         }
  135.  
  136.         DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  137.         drawer.closeDrawer(GravityCompat.START);
  138.         return true;
  139.     }
  140.  
  141.     @Override
  142.     public void onLocationChanged(Location location) {
  143.        
  144.     }
  145.  
  146.     @Override
  147.     public void onStatusChanged(String s, int i, Bundle bundle) {
  148.  
  149.     }
  150.  
  151.     @Override
  152.     public void onProviderEnabled(String s) {
  153.  
  154.     }
  155.  
  156.     @Override
  157.     public void onProviderDisabled(String s) {
  158.  
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement