19 Ocak 2018 Cuma

How To Use Android Services

Android applications have a class called Service for operations that keep running out of sight and don't influence the client's relationship to the application. When we have to make an administration for routine foundation forms, for example, announcing database refreshes, revealing every day mistake reports, we can make a subclass of the Service class and play out the fundamental operations without irritating the client.

In this area we will discuss IntentService, which is a class of Service compose. IntentService, activated by an Activity, executes its activities out of sight without aggravating the client and wrecks itself. For instance, we'll perceive how we can get data from a site out of sight and exchange it to a visual component toward the front (UI Thread) with LocalBroadCastManager.

To begin with make another venture and make the accompanying alters in the AndroidManifest document:


<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.turkcell.backgroundservice" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.turkcell.backgroundservice.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=".HelloService" android:exported="false" /> </application> <uses-permission android:name="android.permission.INTERNET" /> </manifest>



When we associate with a remote web benefit, we have to get web authorization from the application. We have to characterize the administration as an administration all together for the administration to run easily on the application side. We surrendered the name HelloService to make. The traded property here has a (false) esteem to keep the administration from being activated from outside the application.

After this point, how about we make the HelloService class as takes after:



package com.turkcell.backgroundservice; import android.app.IntentService; import android.content.Intent; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; public class HelloService extends IntentService { private static final String serviceName = "HelloService"; public HelloService() { super(serviceName); } @Override protected void onHandleIntent(Intent intent) { String url = intent.getData().toString(); Log.d("HelloService", url); String urlContent = ConnectionHelper.getUrl(url); Log.d("HelloService", urlContent); Intent serviceIntent = new Intent(serviceName); serviceIntent.putExtra("urlContent", urlContent); LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(serviceIntent); } }



The IntentService class requests that we play out the onHandleIntent technique. In this strategy, we composed a code that downloaded a URL and imparted it to the principle screen by means of LocalBroadCastManager. The web address for downloading the substance is incorporated with the sent Intent and downloaded by means of the Internet utilizing the ConnectionHelper class we utilized as a part of the past areas. At that point, the substance of the administration were added to the new Intent with the urlContent key, and LocalBroadcastManager was called into the application as "HelloService". Here, the enlisted strategies that tune in to this notice in the application will have the capacity to peruse the urlContent esteem in the got message.

At long last, we should take a gander at the MainActivity class:


package com.turkcell.backgroundservice; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Bundle; import android.support.v4.content.LocalBroadcastManager; import android.webkit.WebView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(MainActivity.this, HelloService.class); intent.setData(Uri.parse("http://www.turkcell.com.tr")); startService(intent); LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(mMessageReceiver, new IntentFilter("HelloService")); } @Override protected void onDestroy() { super.onDestroy(); LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(mMessageReceiver); } private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { WebView webview = (WebView) findViewById(R.id.webview); webview.getSettings().setJavaScriptEnabled(true); webview.loadData(intent.getStringExtra("urlContent"), "text/html;", null); } }; }

Under onCreate we can trigger another administration with startService. Here we give the web address (URL) in which the administration will download the substance of the Intent by means of the setData technique. Now, the code in HelloService will pull the substance of the address turkcell.com and issue a LocalBroadcast cautioning. We make a BroadcastReceiver to catch the approaching caution and record it as in the past segments. HelloService will send the HTML code from Turkcell site by means of Intent as a String in the wake of finishing the asked for operations. We will catch this String esteem utilizing the intent.getStringExtra strategy and offer it to a WebView that we made on the design record. The design record utilized as a part of this screen resembles this:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" 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" > <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>

Hiç yorum yok:

Yorum Gönder