如何将值从一个活动传递到另一个类

时间:2017-07-13 21:40:47

标签: java android

我需要将String的值传递给另一个类 从我的活动到使用服务的课程

我尝试过一个界面,但它不能,你只能附加一个片段,我不能尝试,因为它是一个在后台运行的类

我怎么能这样做?

主要活动

   public class SettingsActivity extends AppCompatActivity {

private TriStateToggleButton mNotifications;
private ImageButton mButtonFinish;
private CardView mCardTermsPrivacy;
private CardView mCardLicenses;
private CardView mCardLogOut;
private CardView mCardDonations;
private FirebaseUser mUser;




@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        Window w = getWindow();
        w.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        w.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.acitvity_settings);
    mUser = FirebaseAuth.getInstance().getCurrentUser();

    initPantalla();

    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
            .setDefaultFontPath("fonts/RobotoLight.ttf")
            .setFontAttrId(R.attr.fontPath)
            .build()
    );



}

private void initPantalla(){
    mButtonFinish = (ImageButton) findViewById(R.id.imageButtonRegresaMeet);
    mButtonFinish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
    mNotifications = (TriStateToggleButton) findViewById(R.id.triStateToggleNotifications);
    mCardTermsPrivacy = (CardView) findViewById(R.id.cardViewTemrsPrivacy);
    mCardLicenses = (CardView) findViewById(R.id.cardViewLicenses);
    mCardDonations = (CardView) findViewById(R.id.cardViewDonations);
    mCardLogOut = (CardView) findViewById(R.id.cardViewLogOut);


    mCardTermsPrivacy.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            TermsDialogs termsDialogs = new TermsDialogs();
            termsDialogs.setCancelable(true);
            termsDialogs.show(getSupportFragmentManager(), null);

        }
    });

    mCardLicenses.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EasyLicensesDialog easyLicensesDialog = new EasyLicensesDialog(SettingsActivity.this);
            easyLicensesDialog.setTitle("Licenses");
            easyLicensesDialog.setCancelable(true);
            easyLicensesDialog.show();
        }
    });

    mCardLogOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            FirebaseAuth.getInstance().signOut();
            if (AccessToken.getCurrentAccessToken() != null) {
                LoginManager.getInstance().logOut();
            }
            Intent serviceIntent = new Intent(SettingsActivity.this ,FirebaseBackgroundService.class);
            SettingsActivity.this.stopService(serviceIntent);
            Intent intent = new Intent(SettingsActivity.this, RegisterActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);


        }
    });

    mNotifications.setOnToggleChanged(new TriStateToggleButton.OnToggleChanged() {
        @Override
        public void onToggle(TriStateToggleButton.ToggleStatus toggleStatus, boolean b, int i) {
            switch(toggleStatus){
                case on:

                    break;
                case off:

                    break;
            }
        }
    });


}

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

}

服务类,我需要在此

中传递一个字符串

公共类FirebaseBackgroundService扩展了服务{

private FirebaseUser mUser;
private SharedPreferences preferences;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    FirebaseApp.initializeApp(getApplicationContext());
    mUser = FirebaseAuth.getInstance().getCurrentUser();

if(mUser!= null){

FirebaseUtils.getCHATSOLICITUDRef(mUser.getUid()).addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(final DataSnapshot dataSnapshot, final String s) {

        postNotif();
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

}

}



private void postNotif(){
    Intent notificationIntent = new Intent(getApplicationContext(), UserActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Notification noti = new Notification.Builder(this)
            .setContentTitle("Chat request")
            .setContentText("You have pending chat requests")
            .setSmallIcon(R.drawable.ic_dry_cleaning_with_mineral_spirits)
            .setContentIntent(contentIntent)
            .setSound(soundUri)
            .build();
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    noti.flags |= Notification.FLAG_AUTO_CANCEL;


    notificationManager.notify(0, noti);
}

}

1 个答案:

答案 0 :(得分:0)