我试图检测我的应用程序第一次运行。我使用广播接收器来做到这一点。它适用于 ACTION_PACKAGE_REPLACED。但是当我使用 ACTION_PACKAGE_FIRST_LAUNCH 意图时它不起作用。我使用的是安卓 4.3
这是我的活动
public class MainActivity extends Activity {
BroadcastReceiver broadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
broadcastReceiver = new TestBroadcast();
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_PACKAGE_FIRST_LAUNCH);
intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
intentFilter.addDataScheme("package");
registerReceiver(broadcastReceiver, intentFilter);
}
}
这是我的 AndroidManifest.xml
<receiver android:name="com.example.TestBroadcast" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
和TestBroadcast类
public class TestBroadcast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_PACKAGE_FIRST_LAUNCH)) {
Toast.makeText(context, "Application installed",
Toast.LENGTH_SHORT).show();
}
}
}