发送int值从活动一至活动二

问题描述:

嗨,我想从一个活动发送int价值活动二使用此代码发送int值从活动一至活动二

@Override 
     public void onClick(View v) { 
      new ChromaDialog.Builder() 
        .initialColor(getResources().getColor(R.color.colorAccent)) 
        .colorMode(ColorMode.ARGB) 
        .indicatorMode(IndicatorMode.HEX) 
        .onColorSelected(new OnColorSelectedListener() { 
         @Override 
         public void onColorSelected(@ColorInt int color) { 
          Intent intent = new Intent(MainActivity.this, Hackpage.class); 
          intent.putExtra("intVariableName", color); 
          Toast.makeText(MainActivity.this,"color :"+Integer.toHexString(color),Toast.LENGTH_LONG).show(); 
         } 
        }) 
        .create() 
        .show(getSupportFragmentManager(), "ChromaDialog"); 


     } 
    }); 
} 

,但我在值i得到0

问题
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_hackpage); 
    Texthack = (TextView)findViewById(R.id.hacktext); 
    Intent mIntent = getIntent(); 
    int intValue = mIntent.getIntExtra("intVariableName", 0); 
    Texthack.setBackgroundColor(Color.parseColor("#"+Integer.toHexString(intValue))); 
+0

这是什么问题? –

+0

你的实现似乎正确! – lukaspp

+0

@VladMatvienko我得到0 intValue,而不是颜色值 – pic

//global: 
static final String SOME_ACTION = "com.example.test.myapplicationtest.SOME_ACTION"; 
IntentFilter intentFilter = new IntentFilter(SOME_ACTION); //youre project package name. example "com.example.yourprojectpackagename.SOME_ACTION". 



     @Override 
    public void onClick(View v) { 
     new ChromaDialog.Builder() 
       .initialColor(getResources().getColor(R.color.colorAccent)) 
       .colorMode(ColorMode.ARGB) 
       .indicatorMode(IndicatorMode.HEX) 
       .onColorSelected(new OnColorSelectedListener() { 
        @Override 
        public void onColorSelected(@ColorInt int color) { 
      registerReceiver(mReceiver, intentFilter); 
      Intent intent = new Intent(SOME_ACTION); 
      intent.putExtra("intVariableName", color); 
      sendBroadcast(intent); 
      Toast.makeText(MainActivity.this,"color :"+Integer.toHexString(color),Toast.LENGTH_LONG).show(); 
        } 
       }) 
       .create() 
       .show(getSupportFragmentManager(), "ChromaDialog"); 
    } 
}); 

得到它}

 /// 
private BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     int value = intent.getIntExtra("intVariableName",0); 
     Toast.makeText(getApplicationContext(),""+value,Toast.LENGTH_LONG).show(); 
    } 
}; 

它的效果很好。 将价值发送到现有活动。 (例如MainActivity.java)。 所以你不需要使用Hackpage.class。

看起来你正在获取你的密钥的默认值 检查你的颜色变量是int,它可能很长。 如果是长可以通过getIntent().getLongExtra(your_key, default_value)

+0

感谢您的回复,但不起作用 – pic

+0

您是否检查颜色的te值是否为空?或者它是一个整数? – Meikiem

+0

正如你所说,我得到的默认值为0我的问题是不是与颜色值 – pic