受欢迎的博客标签

Android| Intent Example And Code (c#)

Published

 

1.Android中 intent 显式启动与隐式启动

1.1 显式启动代码(Image Sharing Example)

FirstActivity.java

we create an Intent to send an image by using the following code:

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.IO;

Intent CreateIntent ()
        {   
            var sendPictureIntent = new Intent (Intent.ActionSend);
            sendPictureIntent.SetType ("image/*");            
            var uri = Android.Net.Uri.FromFile (GetFileStreamPath ("monkey.png"));           
            sendPictureIntent.PutExtra (Intent.ExtraStream, uri);

            return sendPictureIntent;
        }

come from:

https://docs.microsoft.com/en-us/xamarin/android/user-interface/controls/action-bar

https://github.com/xamarin/monodroid-samples/blob/main/ShareActionProviderDemo/Activity1.cs

1.2 隐式启动代码

1.2 .1隐式启动代码之清单文件-AndroidManifest.xml
清单文件-AndroidManifest.xml

需要在intent添加过滤器intentfilter。

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".FirstActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SecondActivity">
        <intent-filter>
            <action android:name="com.example.deligence.ACTION_START"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
</application>

<action> 标签中我们指明了当前活动可以响应com.example.deligence.ACTION_START

而<category>标签包含了一些附加信息,更精确地表明了当前活动能够响应的Intent中还可能带有的category.

只有<action>和<category>中的内容
能同时匹配上intent指定的action和category时,这个活动才能响应改intent

 
FirstActivity.java
 

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_layout);
    Button button1 = (Button) findViewById(R.id.button_1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent("com.example.deligence.ACTION_START");
            startActivity(intent);
        }
    });

}

 
android.intent.category.DEFAUL 是一种默认的category,再调用startActivity()方法的时候会自动将这个category添加到intent中

1.2.2.隐式启动代码之属性启动代码

The Android manifest provides a way for you to describe the capabilities of your activity. This is done via Intents and the [IntentFilter] custom attribute. You can specify which actions are appropriate for your activity with the IntentFilter constructor, and which categories are appropriate with the Categories property. At least one activity must be provided (which is why activities are provided in the constructor). [IntentFilter] can be provided multiple times, and each use results in a separate <intent-filter/> element within the <activity/>.

An Launcher is exactly the same as any other app, the only difference is that it specifies a couple of IntentFilters so that Android will recognise it.

For example:

[Activity (Label="Awesome Demo App", MainLauncher=true, Icon="@drawable/myicon")] 
[IntentFilter (new[]{Intent.ActionView}, 
        Categories=new[]{Intent.CategorySampleCode, "my.custom.category"})]
public class MyActivity : Activity
{
}

This example produces the following xml fragment:

<activity android:icon="@drawable/myicon" android:label="Awesome Demo App" 
          android:name="md5a7a3c803e481ad8926683588c7e9031b.MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.SAMPLE_CODE" />
    <category android:name="my.custom.category" />
  </intent-filter>
</activity>

come from:https://docs.microsoft.com/en-us/xamarin/android/platform/android-manifest

Then you will need to create loads of views, diaglogs etc. to support the same features as many of the other Launchers do.

 

Useful links

https://abhiandroid.com/programming/intent-filter

https://www.tutorialspoint.com/android/android_intents_filters.htm