2017-07-13 8 views
-2

に1つのアクティビティから値を渡すために、私はサービスはどのように別のクラス

私はインターフェイスを試してみましたを使用するクラスに、私の活動から別のクラス に価値のいずれかの文字列を渡す必要がありますが、それはできません、フラグメントを添付することはできますが、バックグラウンドで実行されるクラスなので試行できません。

どうすればいいですか?

主活性

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(); 

場合、サービスを(延在し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); 
} 

}

+0

(参照渡しとは対照的に)Javaはネイティブ値を通過します。ここをクリックしてください:http://www.javaworld.com/article/2077424/learn-java/does-java-pass-by-reference-or-pass-by-value.html[1] – Jsleshem

+0

あなたはputExtras()メソッドとgetExtras()メソッドを使ってバンドルしてみてください。うまくいけば、この仕事 –

+0

@felipeアクティビティとバックグラウンドサービスの間で通信したいですか? –

答えて

関連する問題