Android-Intent and Intent Filters

1.intent(意图)可以用来创建启动3种类型的基本情况:①To start an activity:启动一个活动②To start an service③To start an broadcast

2.intent types:-----first:Explicit intent:指的是当你知道你的类的名字,你可以使用显式意图,for instance:start an activity or start a service to download a file in                       background;

        -----second:Implicit intent : 可以不指定一个明确的名字,采用一个明确的action 来指定要执行的操作,当其他的apps获取到这个action后执行所代表的动作,

                      他会在清单文件中看看那个能接受这个action。

  PS:当启动一个service时,必须用显式意图!

3.如果一个隐式意图未被其他的程序处理,在startActivity()方法调用时,程序将崩溃

4.PendingIntent可用作三种情况:①通知信息的Intent②Home Screen的Intent,(app widget:应用程序小部件)③定义将来某个时间执行的代码。e.gAlarmManager

5.PendingIntent.getActivity() for an Intent that starts an Activity.

  •   PendingIntent.getService() for an Intent that starts a Service.

 

  •   PendingIntent.getBroadcast() for a Intent that starts an BroadcastReceiver.

 

6.Alarm Clock(闹钟),创建一个闹钟,使用ACTION_SET_ALARM:

      EXTRA_RINGTONEcontent: URI specifying a ringtone to use with the alarm,or VALUE_RINGTONE_SILENT for no ringtone.

        To use the default ringtone, do not specify this extra.

            EXTRA_VIBRATEA boolean specifying whether to vibrate for this alarm.

      EXTRA_SKIP_UIA boolean specifying whether the responding app should skip its UI when setting the alarm. If true, the app should bypass any                confirmation UI and simply set the specified alarm.

显式

public void createAlarm(String message, int hour, int minutes) {
    Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
            .putExtra(AlarmClock.EXTRA_MESSAGE, message)
            .putExtra(AlarmClock.EXTRA_HOUR, hour)
            .putExtra(AlarmClock.EXTRA_MINUTES, minutes);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />

隐式

<activity ...>
    <intent-filter>
        <action android:name="android.intent.action.SET_ALARM" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

7.Create a timer(定时器)创建一个倒计时定时器ACTION_SET_TIMER(PS:最低支持版本Android4.4)

public void startTimer(String message, int seconds) {
    Intent intent = new Intent(AlarmClock.ACTION_SET_TIMER)
            .putExtra(AlarmClock.EXTRA_MESSAGE, message)
            .putExtra(AlarmClock.EXTRA_LENGTH, seconds)
            .putExtra(AlarmClock.EXTRA_SKIP_UI, true); //true 表示不跳转到定时器界面
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

其他如上...

8.Show all alarms(显示所有闹钟)ACTION_SHOW_ALARMS

<activity ...>
    <intent-filter>
        <action android:name="android.intent.action.SHOW_ALARMS" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

只有一个隐式声明意图

9.Add a calender event(在用户的日历中增加新事件)action:ACTION_INSERT

public void addEvent(String title, String location, Calendar begin, Calendar end) {
    Intent intent = new Intent(Intent.ACTION_INSERT)
            .setData(Events.CONTENT_URI)
            .putExtra(Events.TITLE, title)
            .putExtra(Events.EVENT_LOCATION, location)
            .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin)
            .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}
<activity ...>
    <intent-filter>
        <action android:name="android.intent.action.INSERT" />
        <data android:mimeType="vnd.android.cursor.dir/event" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

 

 

你可能感兴趣的:(Android-Intent and Intent Filters)