Flutter LockTask - android

I want to lockdown a flutter applicatio with the android locktask. I tried several approaches but the setLockTaskPackages method doesn't seem to do anything. At first I tried to call the method in the onCreate function of the MainActivity. Then I tried to call it over a method channel but both approaches didn't work. Here is my onCreate funtion with method channel:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
mDevicePolicyManager = (DevicePolicyManager) this.getSystemService(Context.DEVICE_POLICY_SERVICE);
mAdminName = new ComponentName(this, Receiver_Admin.class);
new MethodChannel(getFlutterView(), "com.packagename/kiosk").setMethodCallHandler(
new MethodCallHandler() {
#Override
public void onMethodCall(MethodCall call, Result result) {
mDevicePolicyManager.setLockTaskPackages(
mAdminName,
new String[]{"com.packagename"}
);
result.success(null);
}
}
);
}
My AndroidMainifest.xml:
<receiver
android:name=".receiver.Receiver_Admin"
android:label="Sehne"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="#xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="#style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- This keeps the window background of the activity showing
until Flutter renders its first frame. It can be removed if
there is no splash screen (such as the default splash screen
defined in #style/LaunchTheme). -->
<meta-data
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Any ideas why nothing happens?
Thanks in advance

Related

Direct share is not working with my app. ChooserTargetService is not called

I referred sample application code (https://github.com/googlesamples/android-DirectShare) and I added ChooserTargetService to my application
public class ShareChooserTargetService extends ChooserTargetService {
#Override
public List<ChooserTarget> onGetChooserTargets(ComponentName targetActivityName,
IntentFilter matchedFilter) {
Log.d("ShareChooserTargetService", "onGetChooserTargets: ");
ComponentName componentName = new ComponentName(getPackageName(),
ShareActivity.class.getCanonicalName());
ArrayList<ChooserTarget> targets = new ArrayList<>();
for (User user : Users.getAll()) {
Bundle extras = new Bundle();
extras.putInt("user_id", user.id);
targets.add(new ChooserTarget(
user.name,
Icon.createWithResource(this, R.mipmap.ic_user),
componentName,
extras));
}
return targets;
}
In Manifest file I added these lines:
<activity
android:name=".ui.ShareActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
<meta-data
android:name="android.service.chooser.chooser_target_service"
android:value=".ShareChooserTargetService" />
</activity>
<service
android:name=".ShareChooserTargetService"
android:label="#string/app_name"
android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
<intent-filter>
<action android:name="android.service.chooser.ChooserTargetService" />
</intent-filter>
</service>
But ShareChooserTargetService is not called, log isn't printed. I run sample application in my phone and it works perfectly but these code in my app isn't working. I am unable to find what is the issue. Can anyone help please?

How to Avoid Launching the Android Application Twice ,Running from Eclipse to real device

I am Running the application from eclipse and it's being launched twice: first time launching the app, then again relaunching after few seconds
My app Splash Screen--->> Main activity(Both Opening twice).
i already tried adding android:launchMode="singleInstance" in my manifest file, but not success.
i have tried 3 different applications from my eclipse still opening twice in my Kitkat ,lollipop real device (created new project that one also opening twice)
EDIT 1 :
Tried adding this line in manifest file but not Success-android:launchMode="singleTop"
please let me know How to solve this issue.
manifest file:
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:largeHeap="true"
android:launchMode="singleInstance"
android:theme="#style/AppTheme2" >
<activity
android:name=".Start"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
My start Activity.java
public class Start extends Activity
{
SessionManagerFor_Signin session;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Session class instance
session = new SessionManagerFor_Signin(getApplicationContext());
// Remove the Title Bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.start);
ImageView Image1=(ImageView)findViewById(R.id.imageView1);
//Animation Bottom to Top
TranslateAnimation animation2 = new TranslateAnimation(0.0f, 0.0f,400.0f, 0.0f);
animation2.setDuration(1000);
animation2.setFillAfter(false);
Image1.startAnimation(animation2);
Thread timer = new Thread()
{
#Override
public void run()
{
try {
sleep(3000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
session.checkLogin();
finish();
}
}
};
timer.start();
//For Full Action bar Color Starts
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.FUllStartColor);
//For Full Action bar Color Ends here
}
#TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
Try adding launchMode "singleTop" in your Manifest to your activity.
<activity
android:name="MyActivity"
android:launchMode="singleTop"
... >
Apply intent filter only in one of your activity. Remove from MainActivity...
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:largeHeap="true"
android:launchMode="singleInstance"
android:theme="#style/AppTheme2" >
<activity
android:name=".Start"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait" >
</activity>
</application>
Remove this from one of the two activities:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This intent-filter indicates to Android which is the Main Activity, and you should have one only.
try this:
android:launchMode="singleTask"
May be this will work.
If it does not work then reinstall eclipse.
Apply the below to your splash screen activity,then clean the project and run again..
Try to register activity in manifest file with complete package name.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Activity B starts before Activity A

I was trying to get a small image to show up before my Main Activity starts. This is my current coding in the android manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.test.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
whatever I do, my splash screen does not start. They start separately, but never together, (I still haven't put a timer in my splash image as I want to check whether it works or not and it isn't working)
Remove
<intent-filter>
<action android:name="com.example.test.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Use below code in splash screen after desired time
Intent intent=new Intent(this,MAINACTIVITY.class);
startActivity(intent);
finish(); //To close splashscreen when MAINACTIVITY loads
The above code starts with splash screen and after some time start your main activity
You should remove the <intent-filter> section from your MainActivity declaration ,
and launch the MainActivity from the splashActivity using a simple intent and startActivity call.
Try this.
<activity
android:name=".Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity" >
</activity>
</application>
Code for your Splash Class-
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
public class Splash extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{ // TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
};
handle.postDelayed(delay,5000);
}
}
its delay next intent 5 second. you can set time according to you.
Use This:
<activity
android:name=".Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity" >
</activity>
</application>
and This :
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent mInHome = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(mInHome);
Splash.this.finish();
}
}, 3000);
}
}

android integrating library app

I am trying to integrate an open source app into my android app. I have made the open source app as a library app and integrated the xml into my android manifest file as well. There are no compile errors.
First Screen is the login screen for the library app and when it is called it is throwing java lang class exception error at:
m_app = (TodoApplication) getApplication();
source code of loginscreen.java:
public class LoginScreen extends Activity {
final static String TAG = LoginScreen.class.getSimpleName();
private TodoApplication m_app;
private Button m_LoginButton;
private BroadcastReceiver m_broadcastReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
m_app = (TodoApplication) getApplication();
// supposed to help with the banding on the green background
findViewById(R.id.loginbackground).getBackground().setDither(true);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.todotxt.todotxttouch.ACTION_LOGIN");
m_broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, TodoTxtTouch.class);
startActivity(i);
finish();
}
};
registerReceiver(m_broadcastReceiver, intentFilter);
m_LoginButton = (Button) findViewById(R.id.login);
m_LoginButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
login();
}
});
//final RemoteClient remoteClient = m_app.getRemoteClientManager()
// .getRemoteClient();
//if (remoteClient.isAuthenticated()) {
switchToTodolist();
//}
}
private void switchToTodolist() {
Intent intent = new Intent(this, TodoTxtTouch.class);
startActivity(intent);
finish();
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(m_broadcastReceiver);
}
void login() {
final RemoteClient client = m_app.getRemoteClientManager()
.getRemoteClient();
if (!client.isAvailable()) {
Log.d(TAG, "Remote service " + client.getClass().getSimpleName()
+ " is not available; aborting login");
Util.showToastLong(m_app, R.string.toast_login_notconnected);
} else {
RemoteLoginTask loginTask = client.getLoginTask();
loginTask.showLoginDialog(this);
}
}
}
Integrated library code in android manifest.xml:
<activity android:name="com.todotxt.todotxttouch.LoginScreen" android:label="#string/app_label"
android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="com.todotxt.todotxttouch.category.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.todotxt.todotxttouch.TodoApplication" />
<activity android:name="com.todotxt.todotxttouch.Filter" android:label="Filter"
android:theme="#android:style/Theme.NoTitleBar" />
<activity android:name="com.todotxt.todotxttouch.Preferences" android:label="#string/set_preferences" />
<activity android:name="com.todotxt.todotxttouch.AddTask" android:label="#string/addtask"
android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity-alias android:name="com.todotxt.todotxttouch.AddTaskShortcut"
android:targetActivity="com.todotxt.todotxttouch.AddTask" android:label="#string/shortcut_addtask_name">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity-alias>
<activity-alias android:name="com.todotxt.todotxttouch.AddTaskShare"
android:targetActivity="com.todotxt.todotxttouch.AddTask" android:label="#string/share_addtask_name">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity-alias>
<activity android:name="com.todotxt.todotxttouch.HelpActivity"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
<activity android:name="com.todotxt.todotxttouch.TodoTxtTouch" android:theme="#android:style/Theme.NoTitleBar"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
Could anyone please help me on understanding the problem.
Let me explain further: I am having a file called Todoapplication.java....so the class exists...it being called from LoginScreen.java as
m_app = (TodoApplication) getApplication();
and that is where I am getting java lang class exception?
Activity.getApplication() returns an instance of the application class that was declared in the manifest in the <application> element. I don't see it in your pasted manifest.
It's not enough to simply have the application class in your app. It must be explicitly designated as one in the manifest.
I maybe getting the wrong end of the stick so I beg the programming gods for forgiveness in advance.
Assuming you are developing in Eclipse, is this not a simple case of having a project in Eclipse with the open source source in, which within the project properties has the option isLibrary ticked.
In YOUR project's properties you can add a library and Eclipse will list the open source one (and any others that have the "isLibrary" checked). Would you not simply select the open source project and add it. Your project will then add the library and build again?
To access the open source project, now a library, you can use "import" statements to access any public methods that are exposed.
A good example of this setup process using an open source library project is Actionbar Sherlock to which I wrote a tutorial youtube that demonstrates visually what I have just written. It can be found at http://www.youtube.com/watch?v=avcp6eD_X2k

Run Android app as Administrator

I'm trying to write a simple app, and follow the ApiDemos sample. My app is crashing on startup. I suspect a problem with my manifest, but I'm not sure. Any ideas please?
public class managerActivity extends DeviceAdminReceiver {
public class Controller extends Activity {
static final int RESULT_ENABLE = 1;
DevicePolicyManager mDPM;
ActivityManager mAM;
ComponentName mDeviceAdminSample;
Button mEnableButton;
Button mDisableButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mAM = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
mDeviceAdminSample = new ComponentName(Controller.this, managerActivity.class);
setContentView(R.layout.main);
}
}
and then in my manifest, I have:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="com.appname.managerActivity$Controller"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</activity>
<!-- Device Admin Samples -->
<receiver android:name="com.appname.managerActivity"
android:label="#string/sample_device_admin"
android:description="#string/sample_device_admin_description"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="#xml/device_admin_sample" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>
Here's the crash:
11-11 15:20:52.310: ERROR/AndroidRuntime(553):
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.appname.manager/com.appname.managerActivity$Controller}:
java.lang.InstantiationException:
com.appname.managerActivity$Controller
The nested class cannot be created from outside of its enclosing class, unless it's static. Move your activity to a standalone class (i. e. not nested), or make the activity class static.

Categories

Resources