将一个Activity共享成一个Action供其他程序调用

假设我们现在写好了一个字典的程序,在输入框输入字母,就能显示他的英语翻译,现在我们将这个程序共享成一个Action,这样其他程序就可以通过调用这个Action来访问这个程序。

字典程序的处理方法如下:


                if (getIntent().getData() != null)
		{
			
			String word = getIntent().getData().getHost();
			String sql = "select chinese from t_words where english=?";
			database = openDatabase();
			Cursor cursor = database.rawQuery(sql, new String[]
			{ word });
			String result = "未找到该单词.";
			if (cursor.getCount() > 0)
			{
				cursor.moveToFirst();
				result = cursor.getString(cursor.getColumnIndex("chinese"));
			}
			textview.setText(result);
		}

为了能够使用这个Action,我们需要在mainfirst中配置:

		<activity android:name=".Main" android:label="@string/app_name">
			<intent-filter>
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />
			</intent-filter>
		</activity>
		<activity android:name=".TranslateWord" android:theme="@android:style/Theme.Dialog">
			<intent-filter>
				<action android:name="net.DICTIONARY" />
				<data android:scheme="dict" />
				<category android:name="android.intent.category.DEFAULT" />
			</intent-filter>
		</activity>
这样通过访问定义的Schema的值,就可以调用这个Action了

在另一个程序中,我们这样调用这个Action:

	public void onClick(View view)
	{
		Intent intent = new Intent("net.DICTIONARY", Uri
				.parse("dict://" + etWord.getText().toString()));

		startActivity(intent);

	}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值