sipp11
5 years ago
5 changed files with 361 additions and 33 deletions
@ -0,0 +1,27 @@
|
||||
package com.example.sensorz |
||||
|
||||
import android.content.BroadcastReceiver |
||||
import android.content.Context |
||||
import android.content.Intent |
||||
import android.util.Log |
||||
import android.widget.Toast |
||||
|
||||
|
||||
private const val TAG = "MyBroadcastReceiver" |
||||
|
||||
class MyBroadcastReceiver : BroadcastReceiver() { |
||||
override fun onReceive(context: Context, intent: Intent) { |
||||
|
||||
StringBuilder().apply { |
||||
append("Action: ${intent.action}\n") |
||||
append("URI: ${intent.toUri(Intent.URI_INTENT_SCHEME)}\n") |
||||
toString().also { log -> |
||||
Log.d(TAG, log) |
||||
Toast.makeText(context, log, Toast.LENGTH_LONG).show() |
||||
} |
||||
} |
||||
|
||||
val intent = Intent(context, SensorService::class.java) |
||||
context.startService(intent) |
||||
} |
||||
} |
@ -0,0 +1,92 @@
|
||||
package com.example.sensorz; |
||||
|
||||
import android.app.Service; |
||||
import android.content.Context; |
||||
import android.content.Intent; |
||||
import android.os.IBinder; |
||||
import android.util.Log; |
||||
|
||||
import androidx.annotation.Nullable; |
||||
|
||||
import java.util.Timer; |
||||
import java.util.TimerTask; |
||||
|
||||
|
||||
public class SensorService extends Service { |
||||
|
||||
public int counter = 0; |
||||
|
||||
public SensorService(Context applicationContext) { |
||||
super(); |
||||
Log.i("HERE", "here I am!"); |
||||
} |
||||
|
||||
public SensorService() { |
||||
} |
||||
|
||||
@Override |
||||
public int onStartCommand(Intent intent, int flags, int startId) { |
||||
super.onStartCommand(intent, flags, startId); |
||||
startTimer(); |
||||
return START_STICKY; |
||||
} |
||||
|
||||
@Override |
||||
public void onDestroy() { |
||||
super.onDestroy(); |
||||
Log.i("EXIT", "ondestroy!"); |
||||
Intent bIntent = new Intent(this, MyBroadcastReceiver.class); |
||||
bIntent.setAction("com.example.sensorz.intent.REBOOT"); |
||||
sendBroadcast(bIntent); |
||||
// Intent bIntent = new Intent(this, Rebot)
|
||||
// Intent broadcastIntent = new Intent(this, Senso.class);
|
||||
// sendBroadcast(broadcastIntent);
|
||||
// Intent bIntent = new Intent("this.is.godsent");
|
||||
// sendBroadcast(bIntent);
|
||||
stoptimertask(); |
||||
} |
||||
|
||||
private Timer timer; |
||||
private TimerTask timerTask; |
||||
long oldTime = 0; |
||||
|
||||
public void startTimer() { |
||||
//set a new Timer
|
||||
timer = new Timer(); |
||||
|
||||
//initialize the TimerTask's job
|
||||
initializeTimerTask(); |
||||
|
||||
//schedule the timer, to wake up every 1 second
|
||||
timer.schedule(timerTask, 1000, 1000); //
|
||||
} |
||||
|
||||
/** |
||||
* it sets the timer to print the counter every x seconds |
||||
*/ |
||||
public void initializeTimerTask() { |
||||
timerTask = new TimerTask() { |
||||
public void run() { |
||||
Log.i("in timer", "in timer ++++ " + (counter++)); |
||||
} |
||||
}; |
||||
} |
||||
|
||||
/** |
||||
* not needed |
||||
*/ |
||||
public void stoptimertask() { |
||||
Log.i("EXIT", "stop Timeer Task!"); |
||||
//stop the timer, if it's not already null
|
||||
if (timer != null) { |
||||
timer.cancel(); |
||||
timer = null; |
||||
} |
||||
} |
||||
|
||||
@Nullable |
||||
@Override |
||||
public IBinder onBind(Intent intent) { |
||||
return null; |
||||
} |
||||
} |
Loading…
Reference in new issue