Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. package com.example.user.project;
  2.  
  3. import android.app.AlarmManager;
  4. import android.app.PendingIntent;
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.provider.Settings;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.widget.TextView;
  13.  
  14. public class MainActivity extends AppCompatActivity {
  15. MyLocationListener myLocationListener = new MyLocationListener();
  16. int a;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_main);
  21. myLocationListener.SetUpLocationListener(this);
  22. AlarmManager manager = (AlarmManager)getSystemService(
  23. Context.ALARM_SERVICE);
  24.  
  25. }
  26.  
  27. Intent createIntent(String action, String extra) {
  28. Intent intent = new Intent(this, BroadcastReceiver.class);
  29. intent.setAction(action);
  30. intent.putExtra("extra", extra);
  31. return intent;
  32. }
  33.  
  34. public void Loc(View view){
  35. Intent intent1 = createIntent("action", "extra 1");
  36. AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
  37. TextView textView1 = (TextView)findViewById(R.id.textView);
  38. TextView textView2 = (TextView)findViewById(R.id.textView2);
  39. TextView textView3 = (TextView)findViewById(R.id.textView3);
  40. try {
  41. a=(int)(System.currentTimeMillis()/1000);
  42. textView3.setText(" "+a%86400/3600+" "+a%3600/60+" "+a%60);
  43. textView1.setText((" "+(myLocationListener.imHere.getLatitude())));
  44. textView2.setText((" "+(myLocationListener.imHere.getLongitude())));
  45. PendingIntent pIntent1 = PendingIntent.getBroadcast(this, 0, intent1, 0);
  46. am.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, pIntent1);
  47. }catch (Throwable e){
  48. System.out.println(e);
  49. }
  50. }
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. package com.example.user.project;
  60.  
  61. /**
  62. * Created by USER on 26.03.2017.
  63. */
  64.  
  65. import android.Manifest;
  66. import android.content.Context;
  67. import android.content.pm.PackageManager;
  68. import android.location.Location;
  69. import android.location.LocationListener;
  70. import android.location.LocationManager;
  71. import android.os.Bundle;
  72. import android.support.v4.app.ActivityCompat;
  73.  
  74. class MyLocationListener implements LocationListener {
  75.  
  76. static Location imHere; // здесь будет всегда доступна самая последняя информация о местоположении пользователя.
  77.  
  78. public static void SetUpLocationListener(Context context) // это нужно запустить в самом начале работы программы
  79. {
  80. LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
  81.  
  82. LocationListener locationListener = new MyLocationListener();
  83. if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
  84. && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  85. // TODO: Consider calling
  86. // ActivityCompat#requestPermissions
  87. // here to request the missing permissions, and then overriding
  88. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  89. // int[] grantResults)
  90. // to handle the case where the user grants the permission. See the documentation
  91. // for ActivityCompat#requestPermissions for more details.
  92. return;
  93. }
  94. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, locationListener); // здесь можно указать другие более подходящие вам параметры
  95.  
  96. imHere = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  97. }
  98.  
  99. @Override
  100. public void onLocationChanged(Location loc) {
  101. imHere = loc;
  102. }
  103. @Override
  104. public void onProviderDisabled(String provider) {}
  105. @Override
  106. public void onProviderEnabled(String provider) {}
  107. @Override
  108. public void onStatusChanged(String provider, int status, Bundle extras) {}
  109. }
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. <?xml version="1.0" encoding="utf-8"?>
  120. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  121. package="com.example.user.project">
  122.  
  123. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  124.  
  125. <application
  126. android:allowBackup="true"
  127. android:icon="@mipmap/ic_launcher"
  128. android:label="@string/app_name"
  129. android:supportsRtl="true"
  130. android:theme="@style/AppTheme">
  131. <activity android:name=".MainActivity">
  132. <intent-filter>
  133. <action android:name="android.intent.action.MAIN" />
  134.  
  135. <category android:name="android.intent.category.LAUNCHER" />
  136. </intent-filter>
  137. </activity>
  138.  
  139. <service
  140. android:name=".MyService"
  141. android:enabled="true"
  142. android:exported="true"></service>
  143. </application>
  144.  
  145. </manifest>
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154. <?xml version="1.0" encoding="utf-8"?>
  155. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  156. xmlns:tools="http://schemas.android.com/tools"
  157. android:id="@+id/activity_main"
  158. android:layout_width="match_parent"
  159. android:layout_height="match_parent"
  160. android:paddingBottom="@dimen/activity_vertical_margin"
  161. android:paddingLeft="@dimen/activity_horizontal_margin"
  162. android:paddingRight="@dimen/activity_horizontal_margin"
  163. android:paddingTop="@dimen/activity_vertical_margin"
  164. tools:context="com.example.user.project.MainActivity">
  165.  
  166. <TextView
  167. android:layout_width="wrap_content"
  168. android:layout_height="wrap_content"
  169. android:text="Hello World!"
  170. android:id="@+id/textView" />
  171.  
  172. <Button
  173. android:id="@+id/button"
  174. android:layout_width="wrap_content"
  175. android:layout_height="wrap_content"
  176. android:onClick="Loc"
  177. android:text="Button"
  178. android:layout_below="@+id/textView3"
  179. android:layout_centerHorizontal="true" />
  180.  
  181. <TextView
  182. android:id="@+id/textView2"
  183. android:layout_width="wrap_content"
  184. android:layout_height="wrap_content"
  185. android:layout_alignParentStart="true"
  186. android:layout_below="@+id/textView"
  187. android:text="TextView" />
  188.  
  189. <TextView
  190. android:id="@+id/textView3"
  191. android:layout_width="wrap_content"
  192. android:layout_height="wrap_content"
  193. android:layout_alignParentStart="true"
  194. android:layout_below="@+id/textView2"
  195. android:text="TextView" />
  196. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement