Android完成非Activity类的活动

时间:2013-01-05 15:16:50

标签: android android-activity android-context

在我的应用中,我正在检查是否有网络连接;如果没有,则启动活动,当连接恢复时,完成该活动。我正在使用广播接收器来确定网络连接,但是当应用程序需要完成该活动时,应用程序崩溃时会给我RestrictedContext error

这是LogCat输出:

01-05 14:22:21.543: E/AndroidRuntime(7064): FATAL EXCEPTION: main
01-05 14:22:21.543: E/AndroidRuntime(7064): java.lang.RuntimeException: Unable to start receiver net.evolution.betting.application.ConnectionChangeReceiver: java.lang.ClassCastException: android.app.ReceiverRestrictedContext
01-05 14:22:21.543: E/AndroidRuntime(7064):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
01-05 14:22:21.543: E/AndroidRuntime(7064):     at android.app.ActivityThread.access$3200(ActivityThread.java:125)
01-05 14:22:21.543: E/AndroidRuntime(7064):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
01-05 14:22:21.543: E/AndroidRuntime(7064):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 14:22:21.543: E/AndroidRuntime(7064):     at android.os.Looper.loop(Looper.java:123)

01-05 14:22:21.543: E/AndroidRuntime(7064):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-05 14:22:21.543: E/AndroidRuntime(7064):     at java.lang.reflect.Method.invokeNative(Native Method)
01-05 14:22:21.543: E/AndroidRuntime(7064):     at java.lang.reflect.Method.invoke(Method.java:521)
01-05 14:22:21.543: E/AndroidRuntime(7064):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-05 14:22:21.543: E/AndroidRuntime(7064):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-05 14:22:21.543: E/AndroidRuntime(7064):     at dalvik.system.NativeStart.main(Native Method)
01-05 14:22:21.543: E/AndroidRuntime(7064): Caused by: java.lang.ClassCastException: android.app.ReceiverRestrictedContext
01-05 14:22:21.543: E/AndroidRuntime(7064):     at net.evolution.betting.application.ConnectionChangeReceiver.onReceive(ConnectionChangeReceiver.java:37)
01-05 14:22:21.543: E/AndroidRuntime(7064):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
01-05 14:22:21.543: E/AndroidRuntime(7064):     ... 10 more

这是类来源:

public class ConnectionChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive( Context context, Intent intent ) {
        ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); 
        List<RunningTaskInfo> activitys = activityManager.getRunningTasks(Integer.MAX_VALUE); 
        boolean isActivityFound = false; 
        for (int i = 0; i < activitys.size(); i++) { 
            if (activitys.get(i).topActivity.toString().equalsIgnoreCase("ComponentInfo{net.evolution.betting.application/net.evolution.betting.application.NotationActivity}")) {
                isActivityFound = true;
            }
        } 

        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(     ConnectivityManager.TYPE_MOBILE );
        if ( activeNetInfo != null && isActivityFound == true) {
            ((NotationActivity)context).finish();
        }else if( activeNetInfo == null){
            Intent i = new Intent("net.evolution.betting.application.NOTATIONACTIVITY");
            i.putExtra("DialogTitle", "Warining!");
            i.putExtra("DialogBody", "It seems like your app is not connected to Internet witch is required.Please check your network connection.");
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

你不能这样做&#34;((NotationActivity)context)&#34;。 &#34;上下文&#34;它是您的Receiver的上下文,而不是您的NotationActivity的上下文。因此,你无法以这种方式投射并获得活动。

有几种方法可以满足您的需求。谷歌&#34;完成广播接收器的活动&#34;。有很多有用的信息可以帮助您找到适合您案例的最佳方式。

答案 1 :(得分:1)

问题在于这一行

 ((NotationActivity)context).finish();

让您的Receiver成为活动的内部类,以便它可以访问finish()方法,或者找到从接收者到活动的另一种通信方式。