Issue
I try to get a variable on my Android App from 1 Activite to an other. Ther for I using Intent but I have a Problem with it and I can´t find any answer for it. When I lauch the programm it say´s me every time 90 no matter waht I do.
MainActivitie
`public class MainActivity extends AppCompatActivity {
public static final String EXTRA_NUMBER = "com.example.akkuapp.EXTRA_NUMBER";
int level;
public TextView battery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
battery = (TextView) this.findViewById(R.id.textakku);
this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
AlarmManager alarmManager = (AlarmManager) MainActivity.this.getSystemService(ALARM_SERVICE);
Intent startServiceIntent = new Intent(MainActivity.this, Hintergrundservice.class);
PendingIntent startServicePendingIntent = PendingIntent.getService(MainActivity.this, 0, startServiceIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
long time = calendar.getTimeInMillis();
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 5, startServicePendingIntent);
}
public final BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
@SuppressLint("SetTextI18n")
@Override
public void onReceive(Context context, Intent intent) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
battery.setText(String.valueOf(level) + '%');
intent.putExtra(EXTRA_NUMBER, level);
}
};
Backgroundservice
` @Override public int onStartCommand(Intent intent, int flags, int startId) {
level = intent.getIntExtra(MainActivity.EXTRA_NUMBER,90);
Toast.makeText(getApplicationContext(), String.valueOf(level), Toast.LENGTH_LONG).show();
Log.d("Hintergrundprozess", String.valueOf(level));
return flags;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
Pictur Hintergrundprozess (Backgroundservice)
Thank you for your Help
Solution
There are many problems here. You are using startActivity()
but passing an Intent
for a Service
(HintergrundService
). To start a Service
you need to call startService()
.
Also, getIntentOld()
is a deprecated method of Intent
and is probably not what you want. You already have the Intent
as it is passed into onStartCommand()
. Just use intent.getIntExtra()
:
intent.getIntExtra(MainActivity.EXTRA_NUMBER, 90);
EDIT: Add more data after seeing more code
You have used AlarmManager
to schedule the triggering of an Intent
to start your Service
. In that Intent
you have not put any extras. This is the reason that your Service
doesn't get the "extra".
When your BroadcastReceiver
is triggered and onReceive()
is called, you extract the battery level from the incoming Intent
and add an "extra" with that value back to the same incoming Intent
. The value doesn't get magically copied into the Intent
that you passed to AlarmManager
. You should really find some tutorials and examples and learn more about how all this works.
In general, if your Service
wants to get the battery level, it can just do this:
IntentFilter ifilter =
new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
You don't need to actually register a BroadcastReceiver
. The battery changed Intent
is a "sticky" Intent
, which means that the last one broadcast is held by the Android framework and you can always ask for the last one that was broadcast.
See https://developer.android.com/training/monitoring-device-state/battery-monitoring for more information about monitoring the battery state.
Answered By - David Wasser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.