-1

i have a broadcastreceiver.class

 public class Myclass extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {

    }
}

i want to call function in broadcastreceiver which is in my activity.please tell me with some code how to made this here is my activity

public class MainActivity extends AppCompatActivity {
    Context context = this;
    private AudioManager myAudioManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        public void myfunction()
        {
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

                PackageManager p = getPackageManager();
                ComponentName componentName = new ComponentName(getApplicationContext(),com.example.faisal.wifiprofile.MainActivity.class);
                p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
                handler.postDelayed(this, 100000);
            }
        }, 100000);
        }
    }
}

here is function name myfunction i want to call this function in broadcastreceiver please tell me good solution or edit my code thanks in advance

comrade
  • 4,590
  • 5
  • 33
  • 48
xtylish
  • 115
  • 7

1 Answers1

0

This should work:

public class Myclass extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
   MainActivity.myfunction();
}

}

public class MainActivity extends AppCompatActivity {
Context context = this;
private AudioManager myAudioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

    public static void myfunction()
    {
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {

            PackageManager p = getPackageManager();
            ComponentName componentName = new ComponentName(getApplicationContext(),com.example.faisal.wifiprofile.MainActivity.class);
            p.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
            handler.postDelayed(this, 100000);
        }
    }, 100000);
    }

}

B-GangsteR
  • 2,534
  • 22
  • 34
  • when i put function in class it give error can not resolve – xtylish Jun 25 '16 at 09:18
  • Updated to probably solve your error. If it does not work, post here full error message you get – B-GangsteR Jun 25 '16 at 14:43
  • public static void myfunction() { Toast.makeText(MainActivity.this, "Now in Ringing Mode", Toast.LENGTH_SHORT).show(); } – xtylish Jun 25 '16 at 16:02
  • here is error in MainActivity.this it say make myfunction not static – xtylish Jun 25 '16 at 16:03
  • @NosheenCh this because you can't access not static fields from static function. use instead : Toast.makeText(context, "Now in Ringing Mode", ... declare variable "static Context context;" in your class, and assign to it "this" in onCreate(). And better update your original code to show it's current state. – B-GangsteR Jun 25 '16 at 16:21