分类: 嵌入式
2010-10-22 14:24:34
程序A的Receiver:

receiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context ctx, Intent intent)
{
Log.i("TestActivity", "===============Broadcast Received:" + intent.getAction());
if (intent.getAction().equals(RESPONSE_ACTION))
{
Uri uri = intent.getData();
String location = "";
if (uri != null)
{
Log.i("TestActivity",
"=======================parameter location:"
+ uri.getQueryParameter("location"));
Log.i("TestActivity", "======================= host:"
+ uri.getHost());
Log.i("TestActivity", "======================= path:"
+ uri.getPath());
location = uri.getQueryParameter("location");
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
File file = new File(location);
try
{
fis = new FileInputStream(file);
bos = new ByteArrayOutputStream();
byte[] data = new byte[512];
int count = -1;
while ((count = fis.read(data)) != -1)
{
bos.write(data, 0, count);
}
String xml = new String(bos.toByteArray());
Log.i("TestActivity", "xml1=" + xml);
Toast.makeText(TestActivity.this, xml, Toast.LENGTH_SHORT).show();
} catch (Exception ex)
{
ex.printStackTrace();
} finally
{
if (fis != null)
try
{
fis.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if (bos != null)
try
{
bos.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(RESPONSE_ACTION);
filter.addCategory(Intent.CATEGORY_DEFAULT);
filter.addDataScheme("http");
registerReceiver(receiver, filter);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Uri uri = Uri.parse("");
Intent in = new Intent();
in.setAction(REQUEST_ACTION);
in.addCategory(Intent.CATEGORY_DEFAULT);
in.setData(uri);
TestActivity.this.sendBroadcast(in);
}
});
@Override
public void onReceive(Context context, Intent intent)
{
if(intent != null)
{
Uri uri = intent.getData();
if(uri != null)
{
Log.i("BGServiceReceiver", "=======================" + uri.getQueryParameter("id"));
intent.setClassName("com.backgroundservice",
"com.backgroundservice.TestBackgroundService");
context.startService(intent);
}
}
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }

}
<receiver android:name="ServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.STORE_REQUEST">action>
<category android:name="android.intent.category.DEFAULT">category>
<data android:scheme="http">data>
intent-filter>
receiver>
Intent in = new Intent();
in.setAction(RESPONSE_ACTION);
in.addCategory(Intent.CATEGORY_DEFAULT);
in.setData(Uri.parse(""));
this.sendBroadcast(in);android.intent.category.DEFAULT" (the CATEGORY_DEFAULT constant). Therefore, activities that are willing to receive implicit intents must include "android.intent.category.DEFAULT" in their intent filters.