Skip to content

Commit 39651bd

Browse files
author
hussienalrubaye
committed
start with os
1 parent 78bc37a commit 39651bd

File tree

6 files changed

+99
-95
lines changed

6 files changed

+99
-95
lines changed

MyTracker/app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
1212
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
1313
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
14-
14+
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
1515
<application
1616
android:allowBackup="true"
1717
android:icon="@drawable/cover"
@@ -45,14 +45,13 @@
4545
android:name=".MapsActivity"
4646
android:label="@string/title_activity_maps"></activity>
4747
<service android:name=".MyServie"></service>
48-
<receiver android:name=".RunithOS">
49-
50-
<intent-filter android:priority="2147483647">
51-
<action android:name="android.intent.action.BOOT_COMPLETED"/>
52-
<category android:name="android.intent.category.DEFAULT"></category>
53-
</intent-filter>
48+
<receiver android:name=".StartOs" >
5449

55-
</receiver>
50+
<intent-filter android:priority="2147483647">
51+
<action android:name="android.intent.action.BOOT_COMPLETED"/>
52+
<category android:name="android.intent.category.DEFAULT"/>
53+
</intent-filter>
54+
</receiver>
5655
</application>
5756

5857
</manifest>

MyTracker/app/src/main/java/com/alrubaye/mytracker/MainActivity.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,9 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
175175
void StartServices(){
176176

177177
//start location track
178-
if (!TrackLocation.isRunning){
179-
TrackLocation trackLocation = new TrackLocation();
180-
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
181178

182-
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, trackLocation);
183-
}
184179
if (!MyServie.IsRunning){
185-
Intent intent=new Intent(this,MyServie.class);
180+
Intent intent=new Intent(getBaseContext(),MyServie.class);
186181
startService(intent);
187182
}
188183

MyTracker/app/src/main/java/com/alrubaye/mytracker/MyServie.java

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
package com.alrubaye.mytracker;
22

33
import android.app.IntentService;
4+
import android.app.Service;
5+
import android.content.Context;
46
import android.content.Intent;
57
import android.location.Location;
8+
import android.location.LocationListener;
9+
import android.location.LocationManager;
10+
import android.os.Bundle;
11+
import android.os.IBinder;
12+
import android.support.annotation.Nullable;
613

714
import com.google.firebase.database.DataSnapshot;
815
import com.google.firebase.database.DatabaseError;
@@ -18,29 +25,50 @@
1825
* Created by hussienalrubaye on 9/26/16.
1926
*/
2027

21-
public class MyServie extends IntentService {
28+
public class MyServie extends Service {
2229
public static boolean IsRunning=false;
2330
DatabaseReference databaseReference;
24-
public MyServie(){
25-
super("MyServie");
31+
public static Location location;
32+
TrackLocation trackLocation;
33+
@Nullable
34+
@Override
35+
public IBinder onBind(Intent intent) {
36+
return null;
37+
}
38+
39+
@Override
40+
public void onCreate()
41+
{
42+
super.onCreate();
2643
IsRunning=true;
2744
databaseReference= FirebaseDatabase.getInstance().getReference();
45+
2846
}
47+
2948
@Override
30-
protected void onHandleIntent(Intent intent) {
49+
public int onStartCommand(Intent intent, int flags, int startId)
50+
{
51+
52+
GlobalInfo globalInfo= new GlobalInfo(this);
53+
globalInfo.LoadData();
54+
trackLocation = new TrackLocation();
55+
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
56+
57+
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, trackLocation);
3158

3259
databaseReference.child("Users").child(GlobalInfo.PhoneNumber).
3360
child("Updates").addValueEventListener(new ValueEventListener() {
3461
@Override
3562
public void onDataChange(DataSnapshot dataSnapshot) {
36-
Location location=TrackLocation.location;
63+
64+
if (location==null)return;
3765
databaseReference.child("Users").
3866
child(GlobalInfo.PhoneNumber).child("Location").child("lat")
39-
.setValue(TrackLocation.location.getLatitude());
67+
.setValue( location.getLatitude());
4068

4169
databaseReference.child("Users").
4270
child(GlobalInfo.PhoneNumber).child("Location").child("lag")
43-
.setValue(TrackLocation.location.getLongitude());
71+
.setValue( location.getLongitude());
4472

4573
DateFormat df= new SimpleDateFormat("yyyy/MM/dd HH:MM:ss");
4674
Date date= new Date();
@@ -55,5 +83,37 @@ public void onCancelled(DatabaseError databaseError) {
5583

5684
}
5785
});
86+
return START_NOT_STICKY;
87+
}
88+
89+
public class TrackLocation implements LocationListener {
90+
91+
92+
public boolean isRunning=false;
93+
public TrackLocation(){
94+
isRunning=true;
95+
location=new Location("not defined");
96+
location.setLatitude(0);
97+
location.setLongitude(0);
98+
}
99+
@Override
100+
public void onLocationChanged(Location location) {
101+
MyServie.location=location;
102+
}
103+
104+
@Override
105+
public void onStatusChanged(String provider, int status, Bundle extras) {
106+
107+
}
108+
109+
@Override
110+
public void onProviderEnabled(String provider) {
111+
112+
}
113+
114+
@Override
115+
public void onProviderDisabled(String provider) {
116+
117+
}
58118
}
59119
}

MyTracker/app/src/main/java/com/alrubaye/mytracker/RunithOS.java

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.alrubaye.mytracker;
2+
3+
import android.content.BroadcastReceiver;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.location.LocationManager;
7+
8+
/**
9+
* Created by hussienalrubaye on 10/3/16.
10+
*/
11+
12+
public class StartOs extends BroadcastReceiver {
13+
@Override
14+
public void onReceive(Context context, Intent intent) {
15+
16+
if (intent.getAction().equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")){
17+
18+
19+
/// only when system start
20+
Intent intentService=new Intent(context,MyServie.class);
21+
context. startService(intentService);
22+
}
23+
}
24+
}

MyTracker/app/src/main/java/com/alrubaye/mytracker/TrackLocation.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)