특정 시간 후에 자동 SMS를 보내는 앱이 있습니다. 항상 백그라운드에서 실행하고 휴대 전화를 다시 시작하면 자동으로 프로세스가 시작됩니다. 그것은 Activity로 확장 할 때 잘 동작했습니다. 서비스로 어떻게 시작해야하는지 친절하게 알려주십시오.android : 서비스 시작 방법
서비스 클래스의 코드
public class MainActivity extends Service {
@Override
public void onCreate() {
//super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
startLocationTracking();
}
private void startLocationTracking()
{
AlarmManager am=(AlarmManager)getSystemService(ALARM_SERVICE);
Intent alarmintent1=new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent sender1=PendingIntent.getBroadcast(MainActivity.this, 100, alarmintent1, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
try {
am.cancel(sender1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("exjfkd"+e);
}
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND,5);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000*180, sender1);
System.out.println("set timer");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
초 클래스
public class AlarmReceiver extends BroadcastReceiver{
long time = 180 * 1000;
long distance = 10;
@SuppressLint("NewApi")
@Override
public void onReceive(final Context context, Intent intent) {
System.out.println("alarm receiver....");
LocationManager locationManager = (LocationManager)context
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria, true);
locationManager.requestLocationUpdates(provider, time,
distance, locationListener);
Location location = locationManager.getLastKnownLocation(provider);
String phoneNo = "+96987978";
String Text = "Latitude = " + location.getLatitude() +" Longitude = " + location.getLongitude();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, Text, null, null);
Log.i("Send SMS", "");
Toast.makeText(context, "message sent", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(context, "SMS faild, please try again.",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
LocationListener locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
}
};
}
AndroidManifest를 파일
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:enabled="true"
android:name="com.example.locationupdates.services.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</service>
<receiver android:name="com.example.locationupdates.AlarmReceiver" >
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
이미이 링크를 통해 묻습니다. 'http://stackoverflow.com/questions/5290141/android-broadcastreceiver -on-startup ' –
다음 링크를 참조하여 도움을 받으실 수 있습니다. http://stackoverflow.com/questions/18299700/start-my-service-on-phone-restart-in-android – MFP