重新启动BroadcastReceiver类中的第一个活动

时间:2015-08-11 09:40:28

标签: android

主要活动

public static void restartFirstActivity()
{
    Intent i = getApplicationContext().getPackageManager()
                                      .getLaunchIntentForPackage(getApplicationContext().getPackageName() );

    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
    startActivity(i);
}

BroadcastReceiver类

import com.example.aa.MainActivity;

public class NetworkChange extends BroadcastReceiver{
    MainActivity ma = new MainActivity();
    ma.restartFirstActivity();
}

我在MainActivity的静态中出错。

1 个答案:

答案 0 :(得分:1)

答案:

import com.example.aa.MainActivity;

public class NetworkChange extends BroadcastReceiver{
    Intent a = new Intent(context, MainActivity.class);
    a.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(a);
}