-1

編集 1

BroadcastReceiver からメソッドを呼び出す必要があり、メソッドは以下の Activity クラスに存在します。

このコードを試してみたNULL_POINTER_EXCEPTIONところ、MainActivity クラスへの参照を作成する場所がわかりました。

私が間違っていることを修正してください。

MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void myTesting(){
    Toast.makeText(MainActivity.this, "Welcome to Activity", Toast.LENGTH_SHORT).show();
}
}

BroadcastReceiver.java

public class BootCompeteReceiver extends BroadcastReceiver {

public Context mContext;
private MainActivity mainActivity;

@Override
public void onReceive(Context context, Intent intent) {
    mContext = context;

    try {
        mainActivity = new MainActivity();
        mainActivity.myTesting();

    } catch (Exception e) {
        Toast.makeText(context, ""+e, Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}
4

3 に答える 3

0

これを試すことができます:

public class MainActivity extends Activity {

    private static volatile int INSTANCE_COUNTER = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        INSTANCE_COUNTER++;

        IntentFilter intentFilter = new IntentFilter("com.your.package.ACTION");
        registerReceiver(mWhateverReceiver, intentFilter);

        if (getIntent().getBooleanExtra("fromYourReceiver", false)) {
            myTesting();
        }
    }

    private void myTesting() {
        // Do something here
    }

    private BroadcastReceiver mWhateverReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            myTesting();
        }
    };

    public static boolean isInstanceExist() {
        return INSTANCE_COUNTER > 0;
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        INSTANCE_COUNTER--;
        unregisterReceiver(mWhateverReceiver);
    }
}

あなたの受信機

public class BootCompeteReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (MainActivity.isInstanceExist()) {
            // There is already one instance of MainActivity, so broadcast this
            // event to trigger the receiver inside MainActivity to do your task
            Intent broadcastIntent = new Intent("com.your.package.ACTION");
            context.sendBroadcast(broadcastIntent);
        } else {
            // There is no instances of MainActivity exist, so start a new one
            // with the action that let the instance know what it should do
            Intent activityIntent = new Intent(context, MainActivity.class);
            activityIntent.putExtra("fromYourReceiver", true);
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(activityIntent);
        }
    }
}
于 2013-11-11T11:01:01.683 に答える
0

そのようなことをしてください:

public class MainActivity extends Activity {

private BroadcastReceiver receiver = new BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {

try {

    MainActivity.this.myTesting();

} catch (Exception e) {
    Toast.makeText(MainActivity.this, ""+e, Toast.LENGTH_LONG).show();
    e.printStackTrace();
}
}


@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

   IntentFilter intFilt = new IntentFilter(Constants.YOUR_BROADCAST_RECEIVER_ACTION);
   registerReceiver(receiver, intFilt);

}

public void myTesting(){
   Toast.makeText(MainActivity.this, "Welcome to Activity", Toast.LENGTH_SHORT).show();
}

@Override
public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(receiver);

}
}
于 2013-11-11T08:05:22.067 に答える
0

あなたはあなたの活動の中startActivityonReceive、そして呼び出すことができます。myTestingonCreate

于 2013-11-11T09:07:27.983 に答える