我有一個BroadcastReceiver其檢查NetworkChange,是否連接到互聯網或沒有。 因此,在我的應用程序中,當網絡斷開或連接時,我想知道哪個活動調用了BroadcastReceiver,以便在顯示通知網絡的警報之後,我可以返回到之前的活動。
要獲得廣播接收器的當前活動使用ActivityManager在廣播接收器,以獲得活動沒有實例
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
Log.i("ActivityManager ", "ActivityManager" + cn.toString());
要與實例更好地使用getter和setter的參訪活動得到活動。
MyApplication.Java:
public class MyApplication extends Application {
private Activity mCurrentActivity = null;
// Gloabl declaration of variable to use in whole app
public static boolean activityVisible; // Variable that will check the
// current activity state
public static boolean isActivityVisible() {
return activityVisible; // return true or false
}
public static void activityResumed() {
activityVisible = true;// this will set true when activity resumed
}
public static void activityPaused() {
activityVisible = false;// this will set false when activity paused
}
public Activity getCurrentActivity(){
return mCurrentActivity;
}
public void setCurrentActivity(Activity mCurrentActivity){
this.mCurrentActivity = mCurrentActivity;
}
}
MainActivity.Java:
public class MainActivity extends AppCompatActivity {
private static TextView internetStatus;
protected MyApplication mMyApp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMyApp = (MyApplication) this.getApplicationContext();
internetStatus = (TextView) findViewById(R.id.internet_status);
// At activity startup we manually check the internet status and change
// the text status
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
changeTextStatus(true);
} else {
changeTextStatus(false);
}
}
// Method to change the text status
public void changeTextStatus(boolean isConnected) {
// Change status according to boolean value
if (isConnected) {
internetStatus.setText("Internet Connected.");
internetStatus.setTextColor(Color.parseColor("#00ff00"));
} else {
internetStatus.setText("Internet Disconnected.");
internetStatus.setTextColor(Color.parseColor("#ff0000"));
}
}
@Override
protected void onPause() {
super.onPause();
MyApplication.activityPaused();// On Pause notify the Application
clearReferences();
}
@Override
protected void onResume() {
super.onResume();
MyApplication.activityResumed();// On Resume notify the Application
mMyApp.setCurrentActivity(this);
}
protected void onDestroy() {
clearReferences();
super.onDestroy();
}
private void clearReferences() {
Activity currActivity = mMyApp.getCurrentActivity();
Log.e("Activity", "Activity" + currActivity);
if (this.equals(currActivity))
mMyApp.setCurrentActivity(null);
}
}
InternetConnector_Receiver.Java:
public class InternetConnector_Receiver extends BroadcastReceiver {
public InternetConnector_Receiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
try {
boolean isVisible = MyApplication.isActivityVisible();
Log.i("Activity is Visible ", "Is activity visible : " + isVisible);
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
Log.i("ActivityManager", "Activity Name:" + cn.toString());
// If it is visible then trigger the task else do nothing
if (isVisible == true) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager
.getActiveNetworkInfo();
// Check internet connection and accrding to state change the
// text of activity by calling method
if (networkInfo != null && networkInfo.isConnected()) {
new MainActivity().changeTextStatus(true);
} else {
new MainActivity().changeTextStatus(false);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
清單許可:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:name="com.internetconnection_demo.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Broadcast receiver declaration in manifest file and make sure to enable it -->
<receiver
android:name="com.internetconnection_demo.InternetConnector_Receiver"
android:enabled="true" >
<intent-filter>
<!-- Intent filters for broadcast receiver -->
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
<action android:name="android.net.wifi.STATE_CHANGE" />
</intent-filter>
</receiver>
</application>
當你的意思是'哪一個觸發了這個',你的意思是發送給你的實體?我不認爲你可以。它必須是這樣做的系統。 –