开发者

On Broadcast Receiver can I open an activity

public class USB extends BroadcastReceiver {

 private static final String TAG = "DetactUSB";


@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

     if (in开发者_C百科tent.getAction().equalsIgnoreCase( "android.intent.action.UMS_CONNECTED"))
        {
                TextView textView = new TextView(context);

                textView.setPadding(10,10,10,10);
                textView.setText("USB connected……….");
                Toast toastView = new Toast(context);
                toastView.setDuration(Toast.LENGTH_LONG);
                toastView.setGravity(Gravity.CENTER, 0,0);
                toastView.setView(textView);
                toastView.show();
                Log.i(TAG,"USB connected..");

        }
        if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_DISCONNECTED"))
        {
                TextView textView = new TextView(context);
                textView.setPadding(10,10,10,10);
                textView.setText("USB Disconnected……….");
                Toast toastView = new Toast(context);
                toastView.setDuration(Toast.LENGTH_LONG);
                toastView.setGravity(Gravity.CENTER, 0,0);
                toastView.setView(textView);
                toastView.show();
        }
    } 

Can I open an activity when USB is connected.


You can start activity from receiver like this:

Intent intent = new Intent(context, activitytobelaunch.class);
intent .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜