문제

How do you pass intent extra from service class to FragmentActivity class?

Service Passing Intent to FragmentActivity

  Intent intent = new Intent(getBaseContext(), Home.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.putExtra("AlarmActivated", 1);

        startActivity(intent);

FragmentActivity Accepting Intent passed from service

Bundle extras = getIntent().getExtras();
        if (extras != null) {
            extras = getIntent().getExtras();
            stopMovementAlarm = extras.getInt("AlarmActivated");
            Toast.makeText(getApplicationContext(), stopMovementAlarm,
                    Toast.LENGTH_LONG).show();
            cd.setText("");
            showMyDialog();
            volumeMax();
            alternatingScreen();

        }
도움이 되었습니까?

해결책 3

I Found My Issue, Passing values between activities gets a little bit more complicated when launchmode SingleTop is thrown into the code.

I needed to put this method in my onResume.

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        // set the string passed from the service to the original intent
        setIntent(intent);

    }

When the activity is re-launched while at the top of the activity stack, onNewIntent() gets called on the existing instance and relaunches the activity so my Intent can refresh.

다른 팁

In the intent you have not specified which activity to launched that is why you are getting the error. You need to specify an activity like below

Intent intent = new Intent(context, Activity.class);

I think the best way will be to use a BroadcastReceiver to pass intent. Check this example http://android-coding.blogspot.in/2011/11/pass-data-from-service-to-activity.html

eidt Try to use getApplicationContext() instead of getBaseContext(). If this wont work try something like this :

//in your activity 

    private MyReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
          Bundle bundle = intent.getExtras();
          if (bundle != null) {
                int datapassed = bundle.getInt("DATAPASSED");

                Toast.makeText(getApplicationContext(), "Broadcast received",
                        Toast.LENGTH_LONG).show();
                cd.setText("");
                showMyDialog();
                volumeMax();
                alternatingScreen();
            }
          }
        }
    };

    @Override
    public void onResume(Bundle savedInstanceState) {
        IntentFilter intentFilter = new IntentFilter(SOME_ACTION);
        MyReceiver mReceiver = new MyReceiver();
        registerReceiver(mReceiver, intentFilter);
    }

//in your service somewhere
        Intent intent = new Intent(getApplicationContext(), Home.class);
        intent.putExtra("DATAPASSED", 1);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        sendBroadcast(intent);

I hope that will solve it :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top