From f83095e99fbafd9d6e991504ab1436c7851fd2e6 Mon Sep 17 00:00:00 2001 From: aNaN9811 <1206393146@qq.com> Date: Thu, 9 May 2019 19:14:24 +0800 Subject: [PATCH] =?UTF-8?q?#8=20#251=20=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- students/soft1714080902206/MainActivity.java | 25 +++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/students/soft1714080902206/MainActivity.java b/students/soft1714080902206/MainActivity.java index db25936b2..fc1e73a1e 100644 --- a/students/soft1714080902206/MainActivity.java +++ b/students/soft1714080902206/MainActivity.java @@ -1,13 +1,18 @@ package com.exampler.mynews; +import android.Manifest; +import android.content.pm.PackageManager; import android.databinding.DataBindingUtil; import android.os.Bundle; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.PopupMenu; import android.view.MenuItem; import android.view.View; +import android.widget.Toast; import com.exampler.mynews.databinding.ActivityMainBinding; @@ -29,7 +34,11 @@ public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_main); - initNews(); + if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.INTERNET) != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.INTERNET}, 1); + } else { + initNews(); + } adapter = new NewsAdapter(newsList); binding.recycleView.setAdapter(adapter); binding.recycleView.setLayoutManager(new LinearLayoutManager(this)); @@ -47,6 +56,20 @@ public void onItemLongOnClick(View view, int pos) { }); } + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + switch (requestCode) { + case 1: + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + initNews(); + } else { + Toast.makeText(this, "You denied the peimission", Toast.LENGTH_SHORT).show(); + } + break; + default: + } + } + public void showPopMenu(View view, final int pos){ PopupMenu popupMenu = new PopupMenu(this,view); popupMenu.getMenuInflater().inflate(R.menu.menu_item,popupMenu.getMenu());