Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Intent service = new Intent(getApplicationContext(), MyService.class);
- getApplicationContext().startService(service);
- public class MyService extends Service {
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- // TODO do something useful
- HFLAG = true;
- //smsHandler.sendEmptyMessageDelayed(DISPLAY_DATA, 1000);
- return Service.START_NOT_STICKY;
- }
- @Override
- public IBinder onBind(Intent intent) {
- // TODO for communication return IBinder implementation
- return null;
- }
- }
- <service
- android:name=".MyService"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- </service>
- public int onStartCommand(Intent intent, int flags, int startId) {
- return START_STICKY;
- }
- Intent intent = new Intent(this, PowerMeterService.class);
- startService(intent);
- public class MyBinder extends Binder {
- public MyService getService() {
- return MyService.this;
- }
- }
- private ServiceConnection m_serviceConnection = new ServiceConnection() {
- public void onServiceConnected(ComponentName className, IBinder service) {
- m_service = ((MyService.MyBinder)service).getService();
- }
- public void onServiceDisconnected(ComponentName className) {
- m_service = null;
- }
- };
- Intent intent = new Intent(this, MyService.class);
- bindService(intent, m_serviceConnection, BIND_AUTO_CREATE);
- private void addNotification() {
- // create the notification
- Notification.Builder m_notificationBuilder = new Notification.Builder(this)
- .setContentTitle(getText(R.string.service_name))
- .setContentText(getResources().getText(R.string.service_status_monitor))
- .setSmallIcon(R.drawable.notification_small_icon);
- // create the pending intent and add to the notification
- Intent intent = new Intent(this, MyService.class);
- PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
- m_notificationBuilder.setContentIntent(pendingIntent);
- // send the notification
- m_notificationManager.notify(NOTIFICATION_ID, m_notificationBuilder.build());
- }
- android:launchMode="singleTop"
- startForeground(NOTIFICATION_ID, m_notificationBuilder.build());
- <service
- android:name="WordService"
- android:process=":my_process"
- android:icon="@drawable/icon"
- android:label="@string/service_name"
- >
- </service>
- public int onStartCommand(Intent intent, int flags, int startId) {
- return START_STICKY;
- }
- android:launchMode="singleTop"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement