ما هو ال(intents) في الاندرويد
Arabic (العربية/عربي) translation by Wareef (you can also view the original English article)
ال(intents) هو موضوع أساسي لمطورين الاندرويد . فمن المستحيل بناء تطبيق اندرويد دون الاتصال مع ال(intent) .في هذا البرنامج التعليمي ، سوف اقوم بتعليمك كل شي عن ال(intents ) من الالف الى الياء
ما هو ال(intents) ؟
في مباراة لكره القدم ، يقوم اعضاء الفريق يتمرير الكرة في جميع انحاء الملعب بهدف ارسال الكرة الى هدف الخصم .يتم تمرير الكره من حارس المرمي الفريق إلى المدافعين من فريقه . وبالتالي ، فانها تجد طريقها الى لاعبي خط الوسط، وإذا كانت الأمور تعمل علي النحو المخطط له ، فان واحد من المهاجمين يرسلها الى شباك الخصم . وهذا على افتراض ان حارس المرمي في الجانب الاخر غير قادر على إبقائها بعيدا !
في الاندرويد، القدرة علي إرسال الرسائل اصبحت ممكنة مع ال( كائن Intent object) .مع مساعدة ال( intents) يمكن لمكونات الاندرويد طلب وظائف من مكونات اندرويد اخرى عند فتح تطبيق الانستجرام علي هاتفك واستخدامه لالتقاط صورة ،انت استخدمت لتو ال (intent). وتساعد ال(intent) أيضا في التواصل بين أجزاء التطبيق ؛ الحركة من الشاشة (النشاط activity ) الى الاخرى اصبح ممكن مع ال(intents).
لننظر في الأمر بهذه الطريقة: جميع المكونات (التطبيقات والشاشات)في جهاز الاندرويد يتم عزلها . الطريقة الوحيدة التي يتواصلون بها مع بعضهم البعض هي من خلال ال(intents).
بدء الانشطة باستخدام ال( Starting Activities With Intents )
كما ذكر سابقا ، يمكنك استخدام ال (intents) لبدء مكونات مختلفه: الانشطة (activities) والخدمات (services) وأجهزه استقبال البث(broadcast receivers).
لبدء نشاط ما (activity) سوف نقوم باستخدام هذه الدالة (startActivity(.intent
فيما يلي مقتطف من الاكواد ( التعليمات برمجية ) يوضح كيفيه بدء نشاط آخر (activity) من ال ( intent) .
1 |
Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class); |
2 |
|
3 |
startActivity(numbersIntent); |
أولا ،نقوم بانشاء كائن جديد ( Intent object) ونقوم بتمرير كلاس ال(NumbersActivity) بداخله . وبالتالي نقوم ببدء نشاط (activity ) جديد باستخدام ال(intent).
انواع ال(Intents)
الاندرويد يدعم نوعين من ال(intent): صريحه وضمنية ( explicit and implicit). عندما يقوم أحد التطبيقات بتعريف المكون الهدف الخاص به في ال(intent)، اذن هو صريح ( explicit intent) . عندما لا يقوم التطبيق بتسميه المكون الهدف ، اذن هو ضمني (implicit intent).
مثال على ال(Intent) الصريح (Explicit)
الكود المتكرر للكود أعلاه مثال علي الصريح explicit intent.انظر اليه مره أخرى
1 |
Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class); |
2 |
|
3 |
startActivity(numbersIntent); |
هنا ، NumbersActivity هو المكون الهدف من ال MainActivity . وهذا يعني ان NumbersActivity هو المكون المعرف الذي سيتم استدعاءه من نظام الاندرويد . من المهم ملاحظه (كما في المثال أعلاه) ، ان الintent الصريحة explicit تستخدم عاده داخل أحد التطبيقات ، لان ذلك يعطي المطورتحكم اكثر على اي كلاس سيتم تشغيله (launched).
مثال على الضمني Implicit Intent
هنا وجود ال intent الضمني(implicit) :
1 |
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.tutsplus.com")); |
2 |
startActivity(intent); |
إذا كان لديك الكود أعلاه في الاكواد الخاص بك ، يمكن للتطبيق الخاص بك بدء تشغيل مكون متصفح ل URL محدد عن طريق ال intent . ولكن كيف يمكن للنظام الاندرويد تحديد المكونات التي يمكن ان تتفاعل مع intent معين ؟
ويمكن تسجيل المكون (component) عبر (intent filter) لإجراء محدد. يمكن تسجيل (intent filter) للمكونات( component) بشكل ثابت في AndroidManifest.xml. هنا هو مثال يقوم بتسجيل مكون كعارض ويب
1 |
<activity android:name=".BrowserActivity" |
2 |
android:label="@string/app_name"> |
3 |
<intent-filter> |
4 |
<action android:name="android.intent.action.VIEW" /> |
5 |
<category android:name="android.intent.category.DEFAULT" /> |
6 |
<data android:scheme="http"/> |
7 |
</intent-filter> |
8 |
</activity> |
استخدام الintents في التطبيق
دعنا نكتب بعض التعليمات البرمجية (الكود )لمشاهدة كيف يعمل بها. في هذا القسم، عليك بناء تطبيق صغير لمحاولة لتجربة كل انماط ال intent . سيكون للتطبيق نموذج (form) صغير لإدخال الاسم الأول والاسم الأخير. عند النقر فوق الزر إرسال (Submit )، كل من القيم التي تم إدخالها سيتم تمريرها إلى نشاط آخر (activity) .سيكون هناك أيضا زر لتشغيل متصفح من اختيارك. سيتم فتح المستعرض (المتصفح ) الذي تم اختياره https://code.tutsplus.com.
فتح "الاندرويد استديو "وتشغيل ال MainActivity . يمكنك تعيين اسم الحزمة (package) إلى com.tutsplus.code.android.droidintent.
سوف تبدا ال MainActivity بالعمل مع بعض الimports وتعريف الكلاس (class declaration):
1 |
package com.tutsplus.code.android.droidintent; |
2 |
|
3 |
import android.content.Intent; |
4 |
import android.net.Uri; |
5 |
import android.support.v7.app.AppCompatActivity; |
6 |
import android.os.Bundle; |
7 |
import android.view.View; |
8 |
import android.widget.Button; |
9 |
import android.widget.EditText; |
10 |
|
11 |
public class MainActivity extends AppCompatActivity {
|
12 |
|
13 |
|
14 |
} |
بعد ذلك سوف تقوم بعمل (اoverride) لدالة ال onCreate() لتهيئة النشاط (activity)مع اي حفظ لل state وال layout الخاص بالنشاط (activity ) (سوف نقوم بإنشاء هذا في وقت لاحق).
1 |
@Override |
2 |
protected void onCreate(Bundle savedInstanceState) {
|
3 |
super.onCreate(savedInstanceState); |
4 |
setContentView(R.layout.activity_main); |
5 |
|
6 |
// button handlers go here |
7 |
|
8 |
} |
اذن سوف تقوم بجلب المرجع(references ) لكل من الازرار (buttons) التي تم تعريفها في ال(layout ) مع ارفاق مستمع(a click listener) لكل منهم
1 |
final Button submitButton = (Button) findViewById(R.id.submit_button); |
2 |
|
3 |
submitButton.setOnClickListener(new View.OnClickListener() {
|
4 |
@Override |
5 |
public void onClick(View v) {
|
6 |
EditText firstName = (EditText) findViewById(R.id.first_name_text); |
7 |
EditText lastName = (EditText) findViewById(R.id.last_name_text); |
8 |
|
9 |
String firstNameString = firstName.getText().toString(); |
10 |
String lastNameString = lastName.getText().toString(); |
11 |
|
12 |
Intent submitIntent = new Intent(MainActivity.this, ShowActivity.class); |
13 |
submitIntent.putExtra("firstNameString", firstNameString);
|
14 |
submitIntent.putExtra("lastNameString", lastNameString);
|
15 |
startActivity(submitIntent); |
16 |
} |
17 |
}); |
18 |
لزر الارسال (Submit button) قمت بوضع OnClickListener الذي يؤدي الى تشغيل اجراء (action) حين يتم الضغط على الزر عندما يحدث النقر , يتم احضار الاسم الاول والاخير( the first and last name) من ال(view ) ، وإرسالها إلى النشاط (activity) التالي: ShowActivity . المكون الهدف معرف بشكل صريح في ال(intent) , مما يجعله مثال على ال( explicit intent).
1 |
final Button browserButton = (Button) findViewById(R.id.browser_button); |
2 |
|
3 |
browserButton.setOnClickListener(new View.OnClickListener() {
|
4 |
@Override |
5 |
public void onClick(View v) {
|
6 |
|
7 |
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://code.tutsplus.com"));
|
8 |
startActivity(intent); |
9 |
} |
10 |
}); |
بالنسبة لزر المتصفح (browser button) , المستمع OnClickListener سوف يقوم بانشاء intent جديد يؤدي الى تشغيل ايا من التطبيقات التي توافق ال(filter) : ال ACTION_VIEW يجب عليه التعامل مع ال web URL, وبعبارة أخرى ، فانه يقوم بتشغيل متصفح الويب. إذا كان لديك أكثر من تطبيق للمتصفح (browser application) مثبت على الجهاز الخاص بك، سيتم مطالبتك بتحديد أحد منهم لتنفيذ الإجراء لفتح موقع ويب. وهذا مثال على ال intentالضمني (implicit) .
التخطيط MainActivity Layout
التخطيط(layout) لل MainActivity ستكون بسيطة جداً لهذا البرنامج التعليمي.
1 |
<?xml version="1.0" encoding="utf-8"?>
|
2 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
3 |
xmlns:app="http://schemas.android.com/apk/res-auto" |
4 |
xmlns:tools="http://schemas.android.com/tools" |
5 |
android:layout_width="match_parent" |
6 |
android:orientation="vertical" |
7 |
android:layout_margin="16dp" |
8 |
android:layout_height="match_parent" |
9 |
tools:context="com.tutsplus.code.android.droidintent.MainActivity"> |
10 |
|
11 |
<TextView
|
12 |
android:id="@+id/first_name_view" |
13 |
android:layout_width="wrap_content" |
14 |
android:layout_height="wrap_content" |
15 |
android:text="Enter first name" /> |
16 |
|
17 |
<EditText
|
18 |
android:id="@+id/first_name_text" |
19 |
android:padding="8dp" |
20 |
android:layout_width="200dp" |
21 |
android:layout_height="wrap_content" /> |
22 |
|
23 |
<TextView
|
24 |
android:id="@+id/last_name_view" |
25 |
android:layout_width="wrap_content" |
26 |
android:layout_height="wrap_content" |
27 |
android:text="Enter last name" /> |
28 |
|
29 |
<EditText
|
30 |
android:id="@+id/last_name_text" |
31 |
android:layout_height="wrap_content" |
32 |
android:layout_width="200dp" |
33 |
android:padding="8dp" /> |
34 |
|
35 |
<Button
|
36 |
android:id="@+id/submit_button" |
37 |
android:layout_width="wrap_content" |
38 |
android:layout_height="wrap_content" |
39 |
android:text="Submit"/> |
40 |
|
41 |
<Button
|
42 |
android:id="@+id/browser_button" |
43 |
android:layout_width="wrap_content" |
44 |
android:layout_height="wrap_content" |
45 |
android:layout_marginTop="50dp" |
46 |
android:layout_marginLeft="100dp" |
47 |
android:text="Open Browser"/> |
48 |
|
49 |
</LinearLayout>
|
هنا لديك اثنين من ال TextView واثنين من ال EditText تشير الى الاسم الاول والاسم الأخير (First Name and Last Name) على التوالي. وهناك أيضا زر لارسال الأسماء، وآخر لبدء تشغيل المتصفح الخاص بك.
قم بانشاء الShowActivity
لاستكمال التطبيق لدينا، نحن بحاجة لإنشاء نشاط (activity) للتعامل مع ال intent الصريح (explicit) المحددة أعلاه. إنشا نشاط (activity ) جديد يسمى ShowActivity وهذا النشاط (activity) حيث سيظهر نتيجة لإدخال الاسم الأول والاسم الاخير
( first name and last name) . هنا ما ينبغي أن تبدو:
1 |
package com.tutsplus.code.android.droidintent; |
2 |
|
3 |
import android.support.v7.app.AppCompatActivity; |
4 |
import android.os.Bundle; |
5 |
import android.widget.TextView; |
6 |
|
7 |
public class ShowActivity extends AppCompatActivity {
|
8 |
|
9 |
@Override |
10 |
protected void onCreate(Bundle savedInstanceState) {
|
11 |
super.onCreate(savedInstanceState); |
12 |
setContentView(R.layout.activity_show); |
13 |
|
14 |
Bundle extras = getIntent().getExtras(); |
15 |
String inputFirstName = extras.getString("firstNameString");
|
16 |
String inputLastName = extras.getString("lastNameString");
|
17 |
|
18 |
TextView showFirstName = (TextView) findViewById(R.id.show_first_name); |
19 |
TextView showLastName = (TextView) findViewById(R.id.show_last_name); |
20 |
|
21 |
showFirstName.setText(inputFirstName); |
22 |
showLastName.setText(inputLastName); |
23 |
} |
24 |
} |
في هذا النشاط (activity)، يمكنك البدء بالحصول على النص (string) الذي تم تمريره من الMainActivity . يمكنك الحصول على مرجع (reference) لل intent الذي سبب تشغيل هذا النشاط (activity) باستخدام الدالة getIntent(). ثم يمكنك الوصول إلى النص (string) التي تم تمريرها إليها باستخدام getExtras().getString(). وأخيراً، بعد الحصول على TextView instances من التخطيط (Layout) ، يمكنك عرض القيم التي حصلت عليها.
تخطيط (Layout) الShowActivity
التخطيط (layout) لهذا النشاط بسيط:
1 |
<?xml version="1.0" encoding="utf-8"?>
|
2 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
3 |
xmlns:app="http://schemas.android.com/apk/res-auto" |
4 |
xmlns:tools="http://schemas.android.com/tools" |
5 |
android:layout_width="match_parent" |
6 |
android:layout_height="match_parent" |
7 |
android:layout_margin="16dp" |
8 |
android:orientation="vertical" |
9 |
tools:context="com.tutsplus.code.android.droidintent.ShowActivity"> |
10 |
|
11 |
<TextView
|
12 |
android:layout_width="wrap_content" |
13 |
android:layout_height="wrap_content" |
14 |
android:text="First Name:"/> |
15 |
|
16 |
<TextView
|
17 |
android:id="@+id/show_first_name" |
18 |
android:layout_width="wrap_content" |
19 |
android:layout_height="wrap_content" /> |
20 |
|
21 |
<TextView
|
22 |
android:layout_width="wrap_content" |
23 |
android:layout_height="wrap_content" |
24 |
android:text="Last Name:"/> |
25 |
|
26 |
<TextView
|
27 |
android:id="@+id/show_last_name" |
28 |
android:layout_width="wrap_content" |
29 |
android:layout_height="wrap_content" /> |
30 |
|
31 |
</LinearLayout>
|
اختبار التطبيق
الآن يمكنك إنشاء التطبيق الخاص بك وجربها على جهاز الاندرويد الخاص بك!
تمرير البيانات باستخدام الBundles
يمكنك أيضا استخدام ال Bundles عند تمرير البيانات عبر ال intents.
ال Bundle كلاس يسمح لك بتخزين بيانات معقدة ويدعم أنواع البيانات مثل النصوص (strings), ، حروف(chars)، ال boolean والعدد الصحيح integer وهلم جرا. هنا هو مثال عن كيف يبدو جزء من MainActivity.java لو استخدمت الBundle .
1 |
// Creating Bundle object
|
2 |
Bundle b = new Bundle(); |
3 |
|
4 |
// Storing data into bundle
|
5 |
b.putString("firstNameString", firstNameString); |
6 |
b.putString("lastNameString", lastNameString); |
7 |
|
8 |
// Creating Intent object
|
9 |
Intent submitIntent = new Intent(MainActivity.this, ShowActivity.class); |
10 |
|
11 |
// Storing bundle object into intent
|
12 |
submitIntent.putExtra(b); |
13 |
startActivity(submitIntent); |
الختام
في هذا البرنامج التعليمي، حصلنا على مقدمة موجزة ال intents لإنشاء أنشطة (activity )في الاندرويد . ونحن ننظر في الفرق بين ال intents الصريحة (explicit) والضمنية(implicit) ، وكتابة كود كمثال بسيط يستخدم كل نوع.
يمكنك قراءة المزيد حول ال intents في Android documentation. . فهم كيفية عملها مهم جداً. كما يمكنك بناء تطبيقات أكثر، سوف تواجه الكثير من الانواع المختلفة من ال intents وطرق استخدامها.
وفي الوقت نفسه، تصفح بعض المنشورات الأخرى في تطوير تطبيقات الاندرويد (Android app development) !


حزمة تطوير تطبيقات الاندرويد Android SDKمقدمة في مكونات هيكل الاندرويد (Introduction to Android Architecture Components)Tin Megali تين ميغالي

جافا Javaتصميم الاندرويد والأنماط: نمط المراقب (Android Design Patterns: The Observer Pattern)Chike Mgbemena شيكي مغبيمينا

اندرويد Androidالروبوت من الصفر: بناء أول تطبيق الروبوت الخاص بك (Android From Scratch: Building Your First Android Application)Gianluca Segato جينلو كاسيجاتو



