Android Alarm Manager Service and Intent service Practical Tutorial | the latest tutorial is now and around Android

Android Alarm Manager Service and Intent service Practical Tutorial

Android Alarm Manager Service and Intent service Practical Tutorial - Hallo sahabat the latest tutorial is now and around Android, Pada Artikel yang anda baca kali ini dengan judul Android Alarm Manager Service and Intent service Practical Tutorial, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel Android Alarm Manager Service and Intent service Practical Tutorial, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Android Alarm Manager Service and Intent service Practical Tutorial
link : Android Alarm Manager Service and Intent service Practical Tutorial

Baca juga


Android Alarm Manager Service and Intent service Practical Tutorial

Android Alarm Manager Service and Intent service Practical Tutorial

Alarm Manager
Cron like service are available in Linux  to schedule jobs is available in Android in the form of AlarmManager. AlarmManager allows you to schedule your application to run at a time in the future
Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast,stock details to get the trend of the day etc .
Alarms have these characteristics:
  1. They let you fire Intents at set times and/or intervals.
  2. You can use them in conjunction with broadcast receivers to start services and perform other operations.
  3. They operate outside of your application, so you can use them to trigger events or actions even when your app is not running, and even if the device itself is asleep.
  4. They help you to minimize your app's resource requirements. You can schedule operations without relying on timers or continuously running background services.


Choice of Alarm manager for your Application
A repeating alarm is a relatively simple mechanism with limited flexibility. It may not be the best choice for your app, particularly if you need to trigger network operations. A poorly designed alarm can cause battery drain and put a significant load on servers.
A common scenario for triggering an operation outside the lifetime of your app is syncing data with a server. This is a case where you might be tempted to use a repeating alarm. But if you own the server that is hosting your app's data, using Google Cloud Messaging (GCM) in conjunction with sync adapter is a better solution than AlarmManager. A sync adapter gives you all the same scheduling options as AlarmManager, but it offers you significantly more flexibility.
IntentService
The IntentService is used to perform a certain task in the background. Once done, the instance of IntentService terminates itself automatically. An example for its usage would be downloading certain resources from the internet.
The IntentService class offers the onHandleIntent() method which will be asynchronously called by the Android system.
If the service should be communicating back to the activity, it can receive an object of type Messenger via the Intent data it receives from the activity. If the Messenger is bound to a Handler in the activity, the service can send objects of type Message to the activity.
A Messenger is parcelable, which means it can be passed to another process and you can use this object to send Messages to the Handler in the activity.
Messenger also provides the method getBinder() which allows to pass a Messenger to the activity. The activity can therefore send Messages to the service.
This approach works for local services running in their own process.
This Demonstrative  App is meant to get updated of messages from a website in form of JSON every 15 seconds .Uses Alarm Manager to trigger repeating alarm to start a Service which downloads message from server at regular intervals.The messages are updated through a form in website manually.
(You can try nawinsandroidtutorial.site90.com/message/Message.php to type message to create message.json file replace local host with the above address)
1.Create  Android project with details as listed in the table below.

Property name
Property value
Project name
SRM_AlarmService
Package name
in.ac.srmuniv.alarmservice
Activity name
MainActivity
Layout xml name
activity_main

2.Copy the code  to the file activity_main.xml in res/layout folder 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="8dp"
        android:text="Live Messages:"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_horizontal"
        android:layout_weight="0.57"
        android:textStyle="italic" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="16dp"
        android:text="Stop Update" />


</LinearLayout>
 3.Copy the code in MainActivity.java. Activity.
packagein.ac.srmuniv.alarmservice;

importjava.io.BufferedReader;
import java.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
import java.io.IOException;
importjava.io.InputStreamReader;
import java.util.Calendar;
import java.util.GregorianCalendar;

import org.json.JSONArray;
importorg.json.JSONException;
import org.json.JSONObject;

importin.ac.srmuniv.alarmservice.R;

importandroid.app.Activity;
importandroid.app.AlarmManager;
importandroid.app.PendingIntent;
importandroid.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
importandroid.os.Messenger;
import android.view.View;
importandroid.widget.Button;
importandroid.widget.TextView;
importandroid.view.View.OnClickListener;
importandroid.widget.Toast;

public class MainActivity extends Activity {

     private PendingIntent pendingIntent;
     GregorianCalendar calendar;
     Button b1;
     private Handler handler = new Handler() {
           public voidhandleMessage(Message message) {
                Object path = message.obj;
                if (message.arg1 == RESULT_OK && path != null) {
                     Toast.makeText(MainActivity.this,
                                "Downloaded" + path.toString(), Toast.LENGTH_LONG)
                                .show();
                     readMessage(path.toString());

                } else {
                     Toast.makeText(MainActivity.this, "Download failed.",
                                Toast.LENGTH_LONG).show();
                }

           };
     };
     AlarmManager alarmManager;
     TextView messageText;

     @Override
     public void onCreate(Bundle savedInstanceState) {

           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
           messageText = (TextView) findViewById(R.id.textView);
           b1 = (Button) findViewById(R.id.button1);
           readMessage("/storage/sdcard/message.json");
           b1.setOnClickListener(new OnClickListener() {

                private boolean flag = true;

                @Override
                public void onClick(View v) {
                     if (flag) {
                           flag = false;
                           b1.setText("Start update");
                           alarmManager.cancel(pendingIntent);
                     } else {
                           flag = true;
                           b1.setText("Stop update");
                           alarmManager.setRepeating(AlarmManager.RTC,
                                     calendar.getTimeInMillis(), 15000, pendingIntent);
                     }
                }
           });
           calendar = (GregorianCalendar) Calendar.getInstance();
           Intent myIntent = new Intent(MainActivity.this, UpdatingService.class);
           Messenger messenger = new Messenger(handler);
           myIntent.putExtra("MESSENGER", messenger);
           myIntent.setData(Uri
                     .parse("http://10.0.2.2/message/message.json"));// nawinsandroidtutorial.site90.com/message
           myIntent.putExtra("urlpath",
                     "http://10.0.2.2/message/message.json");
           pendingIntent = PendingIntent.getService(MainActivity.this, 0,
                     myIntent, 0);

           alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
           alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
                     15000, pendingIntent);

     } // end onCreate

     void readMessage(String path) {
           File myFile = new File(path.toString());// Environment.getExternalStorageDirectory().getPath()+"/message.json");
           FileInputStream fIn;
           try {
                fIn = newFileInputStream(myFile);
                BufferedReader myReader = new BufferedReader(new InputStreamReader(
                           fIn));
                String aDataRow = "";
                String aBuffer = "";

                while ((aDataRow = myReader.readLine()) != null) {
                     aBuffer += aDataRow;

                }
                myReader.close();
                try {
                     JSONObject jObj = new JSONObject(aBuffer);
                     aBuffer = jObj.getString("message");
                } catch (JSONException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
                messageText.setText(aBuffer);
           } catch(FileNotFoundException e) {
                messageText.setText("No file");
                e.printStackTrace();
           } catch (IOException e) {
                messageText.setText("IO Error");
                e.printStackTrace();
           }
     }

}
4.Copy the code in UpdatingService.java. Service.
packagein.ac.srmuniv.alarmservice;

importandroid.app.IntentService;
importandroid.content.Intent;

import java.io.File;
importjava.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
importjava.io.InputStreamReader;
import java.net.URL;

importandroid.app.Activity;
import android.net.Uri;
import android.os.Bundle;
importandroid.os.Environment;
import android.os.Message;
importandroid.os.Messenger;
import android.util.Log;

public class UpdatingService extends IntentService {

     String tag = "Nawin";
     private int result = Activity.RESULT_CANCELED;

     public UpdatingService() {
           super("UpdatingService");
     }

     // Will be called asynchronously be Android
     @Override
     protected voidonHandleIntent(Intent intent) {
           Uri data = intent.getData();
           String urlPath = intent.getStringExtra("urlpath");
           Log.d(tag, urlPath);
           String fileName = data.getLastPathSegment();
           File output = new File(Environment.getExternalStorageDirectory(),
                     fileName);
           if (output.exists()) {
                output.delete();
           }

           InputStream stream = null;
           FileOutputStream fos = null;
           try {

                URL url = new URL(urlPath);
                stream = url.openConnection().getInputStream();
                InputStreamReader reader = new InputStreamReader(stream);
                Log.d(tag, "connection done");
                fos = newFileOutputStream(output.getPath());
                int next = -1;
                while ((next = reader.read()) != -1) {
                     fos.write(next);
                }
                // Sucessfulfinished
                result = Activity.RESULT_OK;

           } catch (Exception e) {
                e.printStackTrace();
           } finally {
                if (stream != null) {
                     try {
                           stream.close();
                     } catch (IOException e) {
                           e.printStackTrace();
                     }
                }
                if (fos != null) {
                     try {
                           fos.close();
                     } catch (IOException e) {
                           e.printStackTrace();
                     }
                }
           }

           Bundle extras = intent.getExtras();
           if (extras != null) {
                Messenger messenger = (Messenger) extras.get("MESSENGER");
                Message msg = Message.obtain();
                msg.arg1 = result;
                msg.obj = output.getAbsolutePath();
                try {
                     messenger.send(msg);
                } catch(android.os.RemoteException e1) {
                     Log.w(getClass().getName(), "Exception sending message", e1);
                }

           }
     }
}

5.Modify Androidmanifest.xml
<?xml version="1.0"encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.ac.srmuniv.alarmservice"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="in.ac.srmuniv.alarmservice.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <service
            android:name="in.ac.srmuniv.alarmservice.UpdatingService"
            android:enabled="true" >
        </service>
    </application>
</manifest>
5.Run the application in the emulator.  














Figure 1 Shows the parsed json message from file read from SD card  downloaded from  server by Intent Service and saved in SD card.Update of message from  server is invoked by Alarm Manager Service every 15 seconds.If file is successfully downloaded a Toast message as shown will appear.
Figure 2 Shows the Alarm Manager Service stopped.Hence update service is also stopped.

6.Use XAMPP server start Apache server after coping the Message.php in htdocs/message  folder.The Message.php PHP file which generates message.json file in server.
<html>
       <body>
              <form action="<?php $_PHP_SELF?>" method="POST">
                     Message:
                     <input type="text" name="Message" />
                     <input type="submit" />
              </form>
       </body>
</html>
<?php
class Message {
       public $message = "";
}

$obj = new Message();

if ($_POST) {
       $fp = fopen('message.json', 'w');
       $value = $_REQUEST['Message'];
       $obj -> message = $value;

       fwrite($fp, json_encode($obj));
       fclose($fp);
       exit();
}
Figure 3 Shows Message.php hosted in local server 
Figure 4 Shows message.json file generated  in local server on submitting message content in text box of Message.php 
CODE EXPLANATION
     Alarm manager is a system service,it set to repeating mode.Makes call to UpdatingService every 15 seconds through Alarm Manager,which would call every 15 seconds even out side the life time of the application .A PendingIntent is used to call the Service.The following code in MainActivity shows the setup of Alarm Manager for scheduling the download task.
 calendar = (GregorianCalendar) Calendar.getInstance();
           Intent myIntent = new Intent(MainActivity.this, UpdatingService.class);
           Messenger messenger = new Messenger(handler);
           myIntent.putExtra("MESSENGER", messenger);
           myIntent.setData(Uri
                     .parse("http://10.0.2.2/message/message.json"));// nawinsandroidtutorial.site90.com/message
           myIntent.putExtra("urlpath",
                     "http://10.0.2.2/message/message.json");
           pendingIntent = PendingIntent.getService(MainActivity.this, 0,
                     myIntent, 0);

           alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
           alarmManager.setRepeating(AlarmManager.RTCcalendar.getTimeInMillis(),
                     15000, pendingIntent);
  The Intent Service would get the URL from MainActivity through intent extras, connects to the server and reads the json file and saves it in to the external storage(SD card).A parcelable Message carrying absolute path of the downloaded json file from server is sends message back to Activity handled by Handler class  .The following code in UpdatingService sends message back to activity
 Bundle extras = intent.getExtras();
           if (extras != null) {
                Messenger messenger = (Messenger) extras.get("MESSENGER");
                Message msg = Message.obtain();
                msg.arg1 = result;
                msg.obj = output.getAbsolutePath();
                try {
                     messenger.send(msg);
                } catch (android.os.RemoteException e1) {
                     Log.w(getClass().getName(), "Exception sending message", e1);
                }
      Message handled by Handler in MainActivity through public void handleMessage(Message message) is the callback method of Handler called when message is send from UpdatingService.The received message contains the absolute path of downloaded json file.The readMessage() function reads the content from File in SD card parses the json "message" and displays in Textview as shown in Figure 1


Demikianlah Artikel Android Alarm Manager Service and Intent service Practical Tutorial

Sekianlah artikel Android Alarm Manager Service and Intent service Practical Tutorial kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Android Alarm Manager Service and Intent service Practical Tutorial dengan alamat link https://kokonghod.blogspot.com/2014/04/android-alarm-manager-service-and.html

0 Response to "Android Alarm Manager Service and Intent service Practical Tutorial"