Java Code Examples for android.app.Service#getApplication()

The following examples show how to use android.app.Service#getApplication() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SnifferAccessoryService.java    From sniffer154 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void handleMessage(Message msg) {
	Service service;
	AppSniffer154 app;
	byte[] data;

	service = mService.get();
	if (service != null) {
		Log.d(TAG, "UsbMessageHandler::handleMessage(), what = "
				+ msg.what);

		app = (AppSniffer154) service.getApplication();
		data = msg.getData().getByteArray(
				RetisAccessoryService.MSG_PAYLAOD);
		switch (msg.what) {
		case MAC_FRAME:
			if (mSessionUri != null) {
				InputStream in = new ByteArrayInputStream(data);
				try {
					SnifferUtils.parseInPkt(in, mSessionUri, app);
				} catch (IOException e) {
					// should never happen
					e.printStackTrace();
				}
			}
			break;
		}
	}
}
 
Example 2
Source File: GithubApplication.java    From android-rxmvp-tutorial with Apache License 2.0 4 votes vote down vote up
public static GithubApplication get(Service service) {
  return (GithubApplication) service.getApplication();
}
 
Example 3
Source File: Util.java    From Float-Bar with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 核心方法,加载最近启动的应用程序 注意:这里我们取出的最近任务为 MAX_RECENT_TASKS +
 * 1个,因为有可能最近任务中包好Launcher2。 这样可以保证我们展示出来的 最近任务 为 MAX_RECENT_TASKS 个
 * 通过以下步骤,可以获得近期任务列表,并将其存放在了appInfos这个list中,接下来就是展示这个list的工作了。
 */
public static void reloadButtons(Service service, List<HashMap<String, Object>> appInfos, int appNumber) {
	int MAX_RECENT_TASKS = appNumber; // allow for some discards
	int repeatCount = appNumber;// 保证上面两个值相等,设定存放的程序个数

	/* 每次加载必须清空list中的内容 */
	appInfos.removeAll(appInfos);

	// 得到包管理器和activity管理器
	final Context context = service.getApplication();
	final PackageManager pm = context.getPackageManager();
	final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

	// 从ActivityManager中取出用户最近launch过的 MAX_RECENT_TASKS + 1 个,以从早到晚的时间排序,
	// 注意这个 0x0002,它的值在launcher中是用ActivityManager.RECENT_IGNORE_UNAVAILABLE
	// 但是这是一个隐藏域,因此我把它的值直接拷贝到这里
	@SuppressWarnings("deprecation")
	final List<ActivityManager.RecentTaskInfo> recentTasks = am.getRecentTasks(MAX_RECENT_TASKS + 1, 0x0002);

	// 这个activity的信息是我们的launcher
	ActivityInfo homeInfo = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).resolveActivityInfo(pm, 0);
	int numTasks = recentTasks.size();
	for (int i = 0; i < numTasks && (i < MAX_RECENT_TASKS); i++) {
		HashMap<String, Object> singleAppInfo = new HashMap<String, Object>();// 当个启动过的应用程序的信息
		final ActivityManager.RecentTaskInfo info = recentTasks.get(i);

		Intent intent = new Intent(info.baseIntent);
		if (info.origActivity != null) {
			intent.setComponent(info.origActivity);
		}
		/**
		 * 如果找到是launcher,直接continue,后面的appInfos.add操作就不会发生了
		 */
		if (homeInfo != null) {
			if (homeInfo.packageName.equals(intent.getComponent().getPackageName()) && homeInfo.name.equals(intent.getComponent().getClassName())) {
				MAX_RECENT_TASKS = MAX_RECENT_TASKS + 1;
				continue;
			}
		}
		// 设置intent的启动方式为 创建新task()【并不一定会创建】
		intent.setFlags((intent.getFlags() & ~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) | Intent.FLAG_ACTIVITY_NEW_TASK);
		// 获取指定应用程序activity的信息(按我的理解是:某一个应用程序的最后一个在前台出现过的activity。)
		final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
		if (resolveInfo != null) {
			final ActivityInfo activityInfo = resolveInfo.activityInfo;
			final String title = activityInfo.loadLabel(pm).toString();
			Drawable icon = activityInfo.loadIcon(pm);

			if (title != null && title.length() > 0 && icon != null) {
				singleAppInfo.put("title", title);
				singleAppInfo.put("icon", icon);
				singleAppInfo.put("tag", intent);
				singleAppInfo.put("packageName", activityInfo.packageName);
				appInfos.add(singleAppInfo);
			}
		}
	}
	MAX_RECENT_TASKS = repeatCount;
}
 
Example 4
Source File: Util.java    From Float-Bar with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * 核心方法,加载最近启动的应用程序 注意:这里我们取出的最近任务为 MAX_RECENT_TASKS +
 * 1个,因为有可能最近任务中包好Launcher2。 这样可以保证我们展示出来的 最近任务 为 MAX_RECENT_TASKS 个
 * 通过以下步骤,可以获得近期任务列表,并将其存放在了appInfos这个list中,接下来就是展示这个list的工作了。
 */
public static void reloadButtons(Service service, List<HashMap<String, Object>> appInfos, int appNumber) {
	int MAX_RECENT_TASKS = appNumber; // allow for some discards
	int repeatCount = appNumber;// 保证上面两个值相等,设定存放的程序个数

	/* 每次加载必须清空list中的内容 */
	appInfos.removeAll(appInfos);

	// 得到包管理器和activity管理器
	final Context context = service.getApplication();
	final PackageManager pm = context.getPackageManager();
	final ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

	// 从ActivityManager中取出用户最近launch过的 MAX_RECENT_TASKS + 1 个,以从早到晚的时间排序,
	// 注意这个 0x0002,它的值在launcher中是用ActivityManager.RECENT_IGNORE_UNAVAILABLE
	// 但是这是一个隐藏域,因此我把它的值直接拷贝到这里
	@SuppressWarnings("deprecation")
	final List<ActivityManager.RecentTaskInfo> recentTasks = am.getRecentTasks(MAX_RECENT_TASKS + 1, 0x0002);

	// 这个activity的信息是我们的launcher
	ActivityInfo homeInfo = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).resolveActivityInfo(pm, 0);
	int numTasks = recentTasks.size();
	for (int i = 0; i < numTasks && (i < MAX_RECENT_TASKS); i++) {
		HashMap<String, Object> singleAppInfo = new HashMap<String, Object>();// 当个启动过的应用程序的信息
		final ActivityManager.RecentTaskInfo info = recentTasks.get(i);

		Intent intent = new Intent(info.baseIntent);
		if (info.origActivity != null) {
			intent.setComponent(info.origActivity);
		}
		/**
		 * 如果找到是launcher,直接continue,后面的appInfos.add操作就不会发生了
		 */
		if (homeInfo != null) {
			if (homeInfo.packageName.equals(intent.getComponent().getPackageName()) && homeInfo.name.equals(intent.getComponent().getClassName())) {
				MAX_RECENT_TASKS = MAX_RECENT_TASKS + 1;
				continue;
			}
		}
		// 设置intent的启动方式为 创建新task()【并不一定会创建】
		intent.setFlags((intent.getFlags() & ~Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) | Intent.FLAG_ACTIVITY_NEW_TASK);
		// 获取指定应用程序activity的信息(按我的理解是:某一个应用程序的最后一个在前台出现过的activity。)
		final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
		if (resolveInfo != null) {
			final ActivityInfo activityInfo = resolveInfo.activityInfo;
			final String title = activityInfo.loadLabel(pm).toString();
			Drawable icon = activityInfo.loadIcon(pm);

			if (title != null && title.length() > 0 && icon != null) {
				singleAppInfo.put("title", title);
				singleAppInfo.put("icon", icon);
				singleAppInfo.put("tag", intent);
				singleAppInfo.put("packageName", activityInfo.packageName);
				appInfos.add(singleAppInfo);
			}
		}
	}
	MAX_RECENT_TASKS = repeatCount;
}