开发者

On Broadcast Receiver can I open an activity

开发者 https://www.devze.com 2023-04-03 10:02 出处:网络
public class USB extends BroadcastReceiver { private static final String TAG = \"DetactUSB\"; @Override

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

精彩评论

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

关注公众号