[英]How do i Inherit the methods from a class and use them in another activity
I have a LoginActivity that checks if user is connected to the internet. 我有一个LoginActivity,用于检查用户是否连接到互联网。 And it works perfectly. 而且效果很好。 But now I feel like my activities are full of too much code and would like to separate the internet checker from the Login Activity but im struggling to perform the method calls. 但是现在我感觉我的活动中充满了太多的代码,想将Internet Checker与Login Activity分开,但是却难以执行方法调用。
This is what I've tried: 这是我尝试过的:
InternetChecker class: InternetChecker类:
public class InternetChecker {
public void checkNet(){
if(connected()){
Snackbar.make(findViewById(R.id.activity_login), "Your Internet Connection is Great",
Snackbar.LENGTH_LONG)
.setAction("Action", null).setActionTextColor(Color.RED).show();
Log.i("TRUE","User is connected");
}else{
Snackbar.make(findViewById(R.id.activity_login), "Your You are not Connected to the internet",
Snackbar.LENGTH_LONG)
.setAction("Action", null).setActionTextColor(Color.RED).show();
Log.i("TRUE","User is not connected");
}
}
private boolean connected(){
ConnectivityManager connectivityManager=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo !=null && activeNetworkInfo.isConnected();
}
}
In this class I am getting an error on findViewById
in the method checkNet();
在此类中,我在方法checkNet();
中的findViewById
上遇到错误checkNet();
in the private boolean connected(){
. 在private boolean connected(){
。
So I solved this by using extends AppCompatActivity
in my InternetChecker
class, the errors ( findViewbyId
disappeared). 因此,我通过在InternetChecker
类中使用extends AppCompatActivity
解决了此问题(错误( findViewbyId
消失了))。 I am just wondering if this is the correct convention and if not what should i extend? 我只是想知道这是否是正确的约定,如果不是,我应该扩展什么?
But my main question is: In my LoginActivity which is meant to inherit the InterneChecker methods: 但是我的主要问题是:在我的LoginActivity中,它旨在继承InterneChecker方法:
I have initialized the InternetChecker Class: 我已经初始化了InternetChecker类:
public class LoginActivity extends AppCompatActivity {
private InternetChecker internetChecker = new InternetChecker();
Then I tried using InternetChecker inside a method named checkInternet
which is in my LoginActivity
class: 然后,我尝试在LoginActivity
类中名为checkInternet
的方法中使用InternetChecker:
private void checkInternet(){
InternetChecker = new InternetChecker();
}
But i am getting the error expression expected on this line: 但是我在这条线上得到了错误表达:
InternetChecker = new InternetChecker();
so i tried the following: 所以我尝试了以下方法:
InternetChecker checkNet = new InternetChecker();
There are no errors this time, but CheckNet has a warning: variable checkNet is never used and when i run the app, the app does not call the InternetChecker
methods and check if user is connected to the internet. 这次没有错误,但是CheckNet发出警告:从未使用变量checkNet,并且在我运行该应用程序时,该应用程序不会调用InternetChecker
方法并检查用户是否已连接到Internet。
Please guide me on how I can inherit the methods in the class InternetChecker
, and apply them to LoginActivity
? 请指导我如何继承类InternetChecker
的方法,并将其应用于LoginActivity
?
NB: My Manifest has permissions set already: 注意:“我的清单”已设置了权限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
This is mySolution based on a combination of the answers below 这是基于以下答案的mySolution
I created a BaseActivity: 我创建了一个BaseActivity:
public class BaseActivity extends AppCompatActivity {
public void checkNet(){
if(connected()){
Snackbar.make(findViewById(R.id.activity_login), "Your Internet Connection is Great",
Snackbar.LENGTH_LONG)
.setAction("Action", null).setActionTextColor(Color.RED).show();
Log.i("TRUE","User is connected");
}else{
Snackbar.make(findViewById(R.id.activity_login), "Your You are not Connected to the internet",
Snackbar.LENGTH_LONG)
.setAction("Action", null).setActionTextColor(Color.RED).show();
Log.i("TRUE","User is not connected");
}
}
private boolean connected(){
ConnectivityManager connectivityManager=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo !=null && activeNetworkInfo.isConnected();
}
}
Then extended the BaseActivity
in LoginActivity
然后在BaseActivity
中扩展LoginActivity
public class LoginActivity extends BaseActivity {
I then created a method inside LoginActivity
that calls checkNet
: 然后,我在LoginActivity
内部创建了一个调用checkNet
:
private void checkInternet(){
checkNet();
}
Then called this method in my LoginActivity's onCreate: 然后在我的LoginActivity的onCreate中调用此方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
checkInternet();
But if there are better ways to this than what I've done here, I would appreciate the answer so I can mark is as the correct answer. 但是,如果有比我在这里做的更好的方法,我将感谢您的回答,因此可以将其标记为正确的答案。
The correct way would be to make a MiscUtils class 正确的方法是制作MiscUtils类
public class MiscUtils{
public static isConnected(Context context){
ConnectivityManager connectivityManager=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo !=null && activeNetworkInfo.isConnected();
}
}
Now let all your activities extend a BaseActivity, which would extend the AppCompatActivity, 现在,让您的所有活动扩展一个BaseActivity,这将扩展AppCompatActivity,
In the BaseActivity, 在BaseActivity中,
public class BaseActivity extends AppCompatActivity{
public void showNoInternetMessage(View view){
Snackbar.make(view, "Your You are not Connected to the internet",
Snackbar.LENGTH_LONG)
.setAction("Action", null).setActionTextColor(Color.RED).show();
Log.i("TRUE","User is not connected");
}
}
Now from all your activities [Since they extend the BaseActivity], you can just call 现在,从您的所有活动中(由于它们扩展了BaseActivity),您只需调用
if(MiscUtils.isConnectedToInternet(this)){
}else{
showNoInternetMessage(//Any view id from this activity);
}
You can make InternetChecket inherint from AppCompatActivity
and then make your activity inherit from InternetChecker
: 您可以从AppCompatActivity
使InternetChecket继承,然后使您的活动从InternetChecker
继承:
public class InternetChecker extends AppCompatActivity{
public void checkNet(){
if(connected()){
Snackbar.make(findViewById(R.id.activity_login), "Your Internet Connection is Great",
Snackbar.LENGTH_LONG)
.setAction("Action", null).setActionTextColor(Color.RED).show();
Log.i("TRUE","User is connected");
}else{
Snackbar.make(findViewById(R.id.activity_login), "Your You are not Connected to the internet",
Snackbar.LENGTH_LONG)
.setAction("Action", null).setActionTextColor(Color.RED).show();
Log.i("TRUE","User is not connected");
}
}
private boolean connected(){
ConnectivityManager connectivityManager=(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo !=null && activeNetworkInfo.isConnected();
}
and then: 接着:
public class LoginActivity extends InternetChecker
Now all methods in InternetChecker will be available to you in LoginActivity. 现在,可以在LoginActivity中使用InternetChecker中的所有方法。 And InternetChecker can call findViewById
because it inherits this method from AppCompatActivity
. 而且InternetChecker可以调用findViewById
因为它从AppCompatActivity
继承了此方法。
You can also make InternetChecker an utility class which is more common and I personally recommend: 您还可以使InternetChecker成为更常见的实用工具类,我个人建议:
public class InternetChecker{
private Context mContext;
public InternetChecker(Context context){
mContext = context;
}
private boolean connected(){
ConnectivityManager connectivityManager=(ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo !=null && activeNetworkInfo.isConnected();
}
And then whenever you want to check connectivity you just do within an activity: 然后,只要您想检查连通性,就可以在一个活动中进行:
InternetChecker checker = new InternetChecker(this);
if(checker.connected()){
//connected
}
Instead of keeping it inside an activity, you can create a common class(Common.java). 您可以创建一个公共类(Common.java),而不是将其保留在活动中。 There create this method as a static method. 将该方法创建为静态方法。
public static boolean connected(Activity activity){
ConnectivityManager connectivityManager=(ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo !=null && activeNetworkInfo.isConnected();
}
Wherever you need this method call it like below. 无论何时需要此方法,都可以像下面这样调用它。
Common.connected(activity);
There are 2 ways 1.You need to pass reference of your view in checkNet(View view) method and use this code 有两种方法1.您需要在checkNet(View view)方法中传递视图的引用并使用此代码
public class InternetChecker {
public void checkNet(View view,Activity activity) {
if (connected(activity)) {
Snackbar.make(view.findViewById(R.id.activity_login), "Your Internet Connection is Great",
Snackbar.LENGTH_LONG)
.setAction("Action", null).setActionTextColor(Color.RED).show();
Log.i("TRUE", "User is connected");
} else {
Snackbar.make(view.findViewById(R.id.activity_login), "Your You are not Connected to the internet",
Snackbar.LENGTH_LONG)
.setAction("Action", null).setActionTextColor(Color.RED).show();
Log.i("TRUE", "User is not connected");
}
}
private boolean connected(Activity activity) {
ConnectivityManager connectivityManager = (ConnectivityManager)activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}
Use interface for interaction 使用界面进行交互
public class InternetChecker { 公共类InternetChecker {
public void checkNet(snckbarInterFace snckbarInterFace,Activity activity ) { if (connected()) { snckbarInterFace.showConnected(); } else { snckbarInterFace.showNoConnected(); } }
} }
and from your Login activity call method like this 并从您的登录活动调用方法中像这样
View rootView = getWindow().getDecorView().getRootView();
new InternetChecker().checkNet(rootView);
Create a separate class which just checks for internet connection. 创建一个单独的类,仅检查互联网连接。 And if you want to show snack bar then show in individual activity. 如果您想展示小吃店,那么请参加个人活动。
Code of Internet connection class: 互联网连接类别代码:
public class InternetConnection
{
private Context con;
private static InternetConnection ic = null;
public static synchronized InternetConnection getInstanceInternet(Context con)
{
if (ic == null)
{
ic = new InternetConnection(con);
}
return ic;
}
private InternetConnection(Context con)
{
this.con = con;
}
public boolean isNetworkAvailable()
{
ConnectivityManager connectivityManager = (ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}
Usage of Above class: Create a global object of the class and initialize it in on create of the activity. 上述类的用法:创建该类的全局对象,并在创建活动时对其进行初始化。 Then just check for it like this: 然后像这样检查它:
if (internetConnection.isNetworkAvailable())
{
/// internet is avalable
}
else
{
/// internet not available
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.