android - 有哪些替代方法可以启动我的应用程序?

标签 android android-intent intentfilter android-launcher

我可以通过以下方式启动我的应用:

  1. 在启动器中点击它的图标
  2. 注册“可见的”intent-filter(我的意思是 - 用户点击例如“发送...”然后选择我的应用程序)
  3. 在拨号器和“调用”中输入数字代码 - “隐形” Intent ,用户无法选择应用程序,他只需输入代码

还有其他方法可以启动我的应用程序吗? (我最感兴趣的是其他内容,例如第 3 段中的“隐形” Intent )。

  • 假设我们的设备只有默认系统应用(最流行的 Google 应用也算作默认应用)和我的应用
  • 首选适合普通用户的方法,但更难的方法也会有用
  • 首选可以在一台设备上使用的变体(不需要其他设备),但“多于一个设备的变体”也很有用。

最佳答案

您还可以从网络浏览器运行您的应用:

<intent-filter>
<data android:scheme="my.special.scheme" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>

您可以在 NFC 交易中启动您的应用:

进入mainfest <uses-feature android:name="android.hardware.nfc" />

在这里阅读更多相关信息:LINK


您还可以注册接收器并在收到带有密码的短信时启动应用程序:

  public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();

            Object messages[] = (Object[]) bundle.get("pdus");
            SmsMessage smsMessage[] = new SmsMessage[messages.length];
            for (int n = 0; n &lt; messages.length; n++) {
            smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
            }

            String text = smsMessage[0].getMessageBody();

if(text = "yoursecretcode") {
//launch the app 
abortBroadcast(); //if you want to hide this messeage
 } 
            }

所需权限:<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>


您还可以在收到来自所选电话号码的电话时注册接收器并启动应用程序:

public class ServiceReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context context, Intent intent) {
    MyPhoneStateListener phoneListener=new MyPhoneStateListener();
    TelephonyManager telephony = (TelephonyManager) 
    context.getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
  }
}

public class MyPhoneStateListener extends PhoneStateListener {
  public void onCallStateChanged(int state,String incomingNumber){
  switch(state){

    case TelephonyManager.CALL_STATE_RINGING:
      String numer = TelephonyManager.EXTRA_INCOMING_NUMBER;
   // launch your app if 'numer' is ...

 break;
        }
      } 
    }

你需要这个 READ_PHONE_STATE 权限


您也可以使用 shell 来执行此操作(手机必须已 root):

例如:

Runtime.getRuntime().exec("su");

Runtime.getRuntime ().exec ("am start -n com.android.calculator2/.Calculator");

同事 "Arpan" 写道:

Tilt Your Phone and Wave your Hand (Basically using a Proximity Sensor to launch App's Intent)

我给你代码示例:

public class SensorActivity extends Service implements SensorEventListener {
  private SensorManager mSensorManager;
  private Sensor mProximity;

  @Override
  public final void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
  }

  @Override
  public final void onAccuracyChanged(Sensor sensor, int accuracy) {
    // Do something here if sensor accuracy changes.
  }

  @Override
  public final void onSensorChanged(SensorEvent event) {
    float distance = event.values[0];
  if(!ss()) // LAUNCH YOUR APP IF ISN't RUNNNING
  }

  @Override
  protected void onResume() {
    // Register a listener for the sensor.
    super.onResume();
    mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
  }

  @Override
  protected void onPause() {
    // Be sure to unregister the sensor when the activity pauses.
    super.onPause();
    mSensorManager.unregisterListener(this);
  }
}

private boolean ss() {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if ("com.packagename.something.ActivityName".equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

"Arpan" 还写道:

Plug any usb devices and put an intent filter in the manifest (If usb host mode available)

public static boolean isConnected(Context context) {
        Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
    }

您可以将其粘贴到 Timer


我编辑了 Arpan 的帖子,我添加了有关 Android® 中的手势搜索的链接。


您可以使用小部件启动应用程序(当用户单击它时,应用程序将启动), 我给你小部件类代码 fragment ,更多你可以找到 here :

package com.helloandroid.countdownexample;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;



public class CountdownWidget extends AppWidgetProvider {


    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
            //called when widgets are deleted
            //see that you get an array of widgetIds which are deleted
            //so handle the delete of multiple widgets in an iteration
            super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context) {
            super.onDisabled(context);
            //runs when all of the instances of the widget are deleted from
            //the home screen
            //here you can do some setup
    }

    @Override
    public void onEnabled(Context context) {
            super.onEnabled(context);
            //runs when all of the first instance of the widget are placed
            //on the home screen
    }



@Override
        public void onClick() {
         //your code to launch application...       
        }

    @Override
    public void onReceive(Context context, Intent intent) {
            //all the intents get handled by this method
            //mainly used to handle self created intents, which are not
            //handled by any other method


            //the super call delegates the action to the other methods

            //for example the APPWIDGET_UPDATE intent arrives here first
            //and the super call executes the onUpdate in this case
            //so it is even possible to handle the functionality of the
            //other methods here
            //or if you don't call super you can overwrite the standard
            //flow of intent handling
            super.onReceive(context, intent);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
                    int[] appWidgetIds) {
            //runs on APPWIDGET_UPDATE
            //here is the widget content set, and updated
            //it is called once when the widget created
            //and periodically as set in the metadata xml

            //the layout modifications can be done using the AppWidgetManager
            //passed in the parameter, we will discuss it later

            //the appWidgetIds contains the Ids of all the widget instances
            //so here you want likely update all of them in an iteration

            //we will use only the first creation run
            super.onUpdate(context, appWidgetManager, appWidgetIds);
    }


}

检查耳机是否插入

每当插入耳机时,都会触发一个 Intent (ACTION_HEADSET_PLUG)。通过 BroadcastReceiver 检查并启动 Acitivity

IntentFilter f = new IntentFilter();
f.addAction(Intent.ACTION_HEADSET_PLUG);
registerReceiver(headsetPlugReceiver, f);

public BroadcastReceiver headsetPlugReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // start new Activity or do something else
    }
};

在 list 中:

<receiver android:name="activity.to.receive.headplug.event">    
  <intent-filter>
    <action android:name="android.intent.action.HEADSET_PLUG" />
  </intent-filter>
</receiver>

关于android - 有哪些替代方法可以启动我的应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14543147/

相关文章:

Android WebView 已开始在 Android 9 上崩溃

android - 使用 Intent URI 启动我的应用程序

android - 如何在android中使grid-view水平滚动

android - OneSignal Android - 功能需要 Restful API key

安卓:Intent.getBooleanArrayExtra(key) 返回 null

android - 如何确保电子邮件应用程序将使用我的应用程序打开文件?

android - 使我的应用程序作为文件选择器对话框中的一个选项可用

android - 如何使用 https 方案在 customtabs 上使用 Intent 过滤器?

android - 使用 Proguard 编译给出 SimException : "local variable type mismatch"

安卓。报警管理器。重复警报不触发,一段时间后,一次触发所有警报