adb -s deviceid shell am broadcast -a android.intent.action.VIEW -n com.mypackage.broadcast/com.mypackage.broadcast.Broadcaster
广播类示例。
import android.content.*;
import android.widget.*;
public final class Broadcaster extends BroadcastReceiver
{
@Override
public final void onReceive(final Context context, final Intent intent) {
intent.setClass(context, Starter.class);
//Note: without this flag android will throw a runtime exception.
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(intent);
} catch (final Exception e) {
Toast.makeText(context, e.getMessage(), 1) .show();
forceStop();
}
forceStop();
}
private final void forceStop() {
clearAbortBroadcast();
//throw new RuntimeException();
System.exit(0);
}
}
public final class Starter extends Activity {
@Override
protected final void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Do something
}
}
别忘了把这个放进你的名单里。
<application
android:noHistory="true"
android:launchMode="singleInstance"
android:excludeFromRecents="true"
android:exported="true"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar">
<!-- RECEIVER -->
<receiver
android:name="com.mypackage.broadcast.Broadcaster"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />
<data android:mimeType="*/*" />
</intent-filter>
</receiver>
...