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

15

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

  1. 在启动器中点击其图标
  2. 注册“可见”意图过滤器(例如用户点击“发送到...”,然后选择我的应用程序)
  3. 在拨号键盘中输入数字代码并“呼叫”-“不可见”意图,用户不能选择应用程序,只能输入代码

是否有其他启动应用程序的方法?(我主要对第3段中的“不可见”意图之外的内容感兴趣)。

  • 假设我们只有一个干净的设备,只安装了默认系统应用程序(Google应用程序中最受欢迎的也被视为默认),以及我的应用程序
  • 更适合普通用户使用的方法,但更困难的方法也将很有用
  • 首选可在一台设备上使用的变体,但“多于一台设备的变体”也将很有用。

如果您已经获取了root权限(这仍然相当干净):http://stackoverflow.com/questions/8873537/terminal-run-dalvikvm-with-am-jar - A--C
2
应用程序也可以从浏览器启动。 - nandeesh
@Ty221 目前对我来说并不是很有帮助,因为我需要能够在一个设备上使用的变体(你建议发送短信和打电话),而且你的网络浏览器变体已经被“nandeesh”在评论中提出。但是,正如你所看到的,你的变体对社区很有帮助,所以请继续在这里发布更多的变体。 - janot
你想要实现什么目标?这是一个非常模糊的问题。 - David Snabel-Caunt
@DavidCaunt 我想找到更多启动应用程序的方法,而不是在启动器中点击它的图标。目前我使用了我提问中的第3种方式。但是在没有拨号器的设备上无法使用。 - janot
显示剩余3条评论
2个回答

14

您也可以从Web浏览器中运行您的应用程序:

<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"写道:

倾斜您的手机并挥手(基本上使用接近传感器来启动应用程序的意图)

以下是代码示例:

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" 也写道:

插入任何 USB 设备并在清单中放置一个意图过滤器(如果可用 USB 主机模式)。

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;
    }

您可以将此粘贴到计时器中。


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


您可以使用小部件启动应用程序(当用户单击此项时,应用程序将启动), 我给您小部件类代码片段,更多信息可以在这里找到:

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);
    }


}

检查耳机是否插入

每当耳机被插入时,会触发一个意图(ACTION_HEADSET_PLUG)。通过BroadcastReceiver检查此意图,并启动Activity

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
    }
};

在清单文件中:

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


谢谢,我需要所有可用的变体。最后,我计划创建社区维基答案,并列出所有建议的变体。 - janot
至少我们可以尝试在这里收集它们的大部分。 - janot
大多数情况下,这些方法只有在用户第一次运行应用程序后才能起作用。 - Leonidos
插入耳机也会触发一个事件,可以用来启动一个活动。 - Martin Grohmann
在尝试从Web浏览器运行应用程序时请注意(我之前不知道):http://stackoverflow.com/a/16445731/1548085 - janot
如果 (text = "thesecretcode"),启动应用程序所需的实际代码是什么? - AJW

2
  1. 倾斜您的手机并挥手(基本上使用接近传感器启动应用程序的意图)
  2. 屏幕轻点和/或手势来启动一个意图(您可以在这里阅读有关此内容的信息)
  3. 插入任何USB设备并在清单中添加意图过滤器(如果支持USB主机模式)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接