I've written following class to start my application activity Home.class but on device start up it shows error forced close.
public class MyBootRecvr extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, Home.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_FROM_BACKGROUND);
context.startActivity(i);
Toast.makeText(context, "Where is my KeyBoard", Toast.LENGTH_LONG)
.show();
}
}
Permissions and receiver tags in application.
<receiver
android:name=".Home"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
If this is the only code in your application, it will never run. Android applications must have at least one subclass of Activity as a starting point for when you run the app. The Activity could should at a minimum look something like this:
class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.my_layout); /* my_layout should be an XML layout in res/layout */
}
}
Make sure the following code in is your AndroidManifest.xml file
<activity android:name="MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
If you create a new Android project in Eclipse, it will do this setting up for you. There are a lot of tutorials on both setting up a basic application in Android, and using Eclipse to do it for you.
doing this solve the problem
thanks to xono
<receiver
android:name=".MyBootRecvr"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Related
I have an application, that can be successfully setup as Device Owner on devices up to Android 12 via QR code from JSON below:
{
"android.app.extra.PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME":
"package.CustomDeviceAdminReceiver",
"android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM":
"actual_checksum",
"android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION":
"https://Site/APK_Link",
"android.app.extra.PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED": true
}
App contains declared receiver:
<receiver
android:name=".deviceadmin.CustomDeviceAdminReceiver"
android:description="#string/app_name"
android:label="#string/app_name"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="#xml/enterprise_device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.PROFILE_PROVISIONING_COMPLETE" />
</intent-filter>
</receiver>
For Android 12 (as described here https://source.android.com/devices/tech/admin/provision) I added 2 activities:
<activity
android:name=".deviceadmin.AdminPolicyComplianceActivity"
android:screenOrientation="portrait"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<intent-filter>
<action android:name="android.app.action.ADMIN_POLICY_COMPLIANCE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".deviceadmin.ProvisioningModeActivity"
android:screenOrientation="portrait"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<intent-filter>
<action android:name="android.app.action.GET_PROVISIONING_MODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
1st one:
public class ProvisioningModeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_provisioning_mode);
Intent intent = getIntent();
int provisioningMode = 1;
List<Integer> allowedProvisioningModes = intent.getIntegerArrayListExtra(DevicePolicyManager.EXTRA_PROVISIONING_ALLOWED_PROVISIONING_MODES);
if (allowedProvisioningModes.contains(DevicePolicyManager.PROVISIONING_MODE_FULLY_MANAGED_DEVICE))
provisioningMode = DevicePolicyManager.PROVISIONING_MODE_FULLY_MANAGED_DEVICE;
else if (allowedProvisioningModes.contains(DevicePolicyManager.PROVISIONING_MODE_MANAGED_PROFILE))
provisioningMode = DevicePolicyManager.PROVISIONING_MODE_MANAGED_PROFILE;
Intent resultIntent = new Intent();
resultIntent.putExtra(DevicePolicyManager.EXTRA_PROVISIONING_MODE, provisioningMode);
setResult(RESULT_OK, resultIntent);
finish();
}
}
and 2nd one (almost empty):
public class AdminPolicyComplianceActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_policy_compliance);
setResult(RESULT_OK);
finish();
}
}
but I got error while enrollment: "Can't setup device. Can't use the admin app. It's missing components or corrupted".
Can somebody find that I missed please?
From Android 12 we are supposed to have components exported safely. Since your activities ProvisioningModeActivity and AdminPolicyComplianceActivity uses intent filter, we have to set the exported flag.
<activity
android:name=".deviceadmin.AdminPolicyComplianceActivity"
android:screenOrientation="portrait"
android:permission="android.permission.BIND_DEVICE_ADMIN"
android:exported="true">
<intent-filter>
<action
android:name="android.app.action.ADMIN_POLICY_COMPLIANCE"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".deviceadmin.ProvisioningModeActivity"
android:screenOrientation="portrait"
android:permission="android.permission.BIND_DEVICE_ADMIN"
android:exported="true">
<intent-filter>
<action android:name="android.app.action.GET_PROVISIONING_MODE"
/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This way firmware will be able to recognize the activity and launch it.
Also, regarding android:testOnly="false", this flag must be false if you are going to do QR code provisioning. If its true then, you will be able to remove admin via android settings.
#faz the reason this code doesn't work on Android 11 and below is because the ProvisioningModeActivity intent is not fired with an extra integer array of DevicePolicyManager.EXTRA_PROVISIONING_ALLOWED_PROVISIONING_MODES. To overcome this add a null check to the allowedProvisioningModes
In my App, it opens a Splash Screen then MainActivity. I wrote the following code
SplashActivity.java
public class SplashActivity extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
MainActiviy.java
here
And I added both MainActivity and SplashActivity to manifest as following:
<activity
android:name="com.emy.healthytips.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.emy.healthytips.MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation|screenSize"
android:launchMode="singleTop">
<meta-data
android:name="android.app.default_searchable"
android:value=".MainActivity" />
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="com.emy.healthytips.MainActivity"
android:scheme="oauth" />
</intent-filter>
</activity>
But it gives me the following Exception
FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.emy.healthytips.MainActivity
at com.emy.healthytips.SplashActivity$1.run(SplashActivity.java:20)
In this line
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
How can I fix this? Hope anyone can help me.
Thanks in advance.
Multiple things...
1) The way you are doing it defeats the purpose of a splash screen. Splash screens are supposed to give the user a pretty picture while the app loads in the background. All you are doing is adding an extra 2s delay. Take a look at this post: Android SplashScreen
2) Many people say that this method does not work on <4.0. Not sure why, but just a heads up (https://stackoverflow.com/a/5486970/2066079)
3) instead of:
startActivity(intent);
finish();
you should use:
SplashActivity.this.startActivity(intent);
SplashActivity.this.finish();
You want to use the activity's version of startActivity() instead of the Runnable's. This might be unneccessary, but if it doesn't help, atleast it's good practice.
4) Also, like I mentioned in my comment, using android:name=".MainActivity" instead of android:name="com.emy.healthytips.MainActivity" in the xml is preferred to eliminate possible unchecked typo errors.
My application works as a launcher and also it starts on startup. However, something is wrong with it. For instance, I install my application on device, and open it by selecting Always button (as default launcher). There is no problem until here. However, if I reboot my device (it opens on startup, as I said before), the application opens. But when I want to close it, I cannot do that. It opens again.
This is my Manifest file:
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name="com.comeks.cocuktablet.Main"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This is BootUpReceiver.java:
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context, Main.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
You can use the PackageManager to clear your own application of its defaults, put this inside your onCreate():
PackageManager pm = getPackageManager();
pm.clearPackagePreferredActivities("com.your.package.name");
and fill in your own package name. That should clear the launcher default from your app, the next time they press home button they should be shown the choices of which app to use.
The Android app I am building uses a library that is constantly pushing data to a remote server. In a demo MyMainactivity did exactly this without any user interface and the activity worked well.
Now that I am building the UI around this activity with a TabView I am puzzled how to execute MyMainActivity - in my manifest I now have MyTabActivity as LAUNCHER so how can I make MyMainActivity and MyTabActivity both start up on launch? (MyMainActivity should run while the user is able to scroll through the tabs ans at a later stage should have influence on how MyMainActivity sends data to our servers).
<activity
android:name=".MyTabActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can put the MyMainActivity in the first visible tab, so that it gets started when the tab is becoming visible.
Or you can launch MyMainActivity start what you need to do and then forward your app to the MyTabActivity.
Every activity with a GUI has an onCreate method that you override to define what happens when the activity is launched. You would add the code to run background service when MyTabActivity is created.
class MyTabActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
Intent myIntent = new Intent(getApplicationContext(), MyMainActivity.class);
startService(myIntent);
setContentView(viewid);
}
}
You can try by this.
<activity
android:name=".MyTabActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity
android:name=".MyMainactivity " >
</activity>
</activity>
Another way you also try by this way in your tab activity. Here, you use your stuff.
<receiver android:name=".AutoConnection" >
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver
public class AutoConnection extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
How can I start a new application on Android? I have done a new applications NewHomeScreen and Hello and on NewHomeScreen I wrote this code.
#Override public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.main);
Intent mainIntent = new Intent(this,Hello.class);
startActivity(mainIntent);
}
However, it does not start Hello application. Debugger says that state has value null but what should it be? I also wrote this to Manifest:
<activity android:name="Hello">
<intent-filter>
<action android:name="android.intent.action.HELLO" />
<category android:name="android.intent.category.HELLO"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Try getting rid of the intent-filter in your Home activity. You don't need it anyway, if it's not your main screen.
I think you forgot to specify the main Activity in your AndroidManifest.xml file:
<application android:icon="#drawable/icon">
<activity android:name="NewHomeScreen" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.HELLO" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>