[TIP]Multiple Tips Android -- Updated (6/20/2013) - C++ or Other Android Development Languages

How To Load an another Layout
1- First make an another Layout. name it whatever you want. I name it to "Main2"
2- In the OnCreate bundle, type this
Code:
SetContentView(Resource.Layout.Main2);
Main2 is the Layout that you have named.
3- Now just run it.
How to Load an another Activity
1- Create an another activity, name it whatever u want. I named it to "Activity2"
2- In the OnCreate Bundle, type this
Code:
StartActivity(typeof(Activity2));
Activity2 is the name the activity you want to load
How to Finish a Activity
1- In OnCreate Bundle, type this,
Code:
Finish();
2- Then run, it will only finish the activity where you u use the code
Be Creative to use the codes
I'll share with you what I learn

Loading Progress Box
Code:
private ProgressDialog progress;
Code:
progress = new ProgressDialog(this);
progress.SetTitle("Loading...");
progress.SetCancelable(false);
progress.SetMessage("My Testing");
progress.Show();
to dismiss it.
Code:
progress.Dismiss();

Download a file
Code:
using System.Net;
Code:
using (WebClient Client = new WebClient ())
{
Client.DownloadFile("http://www.abc.com/file/song/a.mpeg", @"/mnt/sdcard/my.mpg");
}

Related

[Q] Call method from view to activity

hi, how can i call a method that is in a view.java going to my activity.java? is there a command for that?
please help =(
In general, if the classes can "see" each other, and the method you want to call is accessible by the caller class, then just call the method via an instance of the according class. So let's say there were class A in file A.java with the method a() and you want to call it from class B in B.java you need to make sure that a() is not a private method (declared public, protected or not explicitly declared) and also it were good if the classes shared the same package.
Then just create an instance of A accessible by a method of B. (only necessary if a() isn't static!) For example like:
Code:
class B {
B() {
A objectOfTypeA = new A();
objectOfTypeA.a();
// voila
}
}
So as you can see there's no special command necessary to call a method of another class. Also I recommend you to read some tutorials about controlling access to members of a class.

[Q] NullPointerException when trying to access MainActivity from different class

Hi!
I am trying to develop an android app with a google map v2, location service and some control buttons.
But I don't want to put all these things inside one MainActivity class. So I thought I could split all the code into some more classes. The MainActivity shall controll all the GUI things and react on map or location events...
Now I have the following problem. Inside my onCreate I instanziate the additional classes:
Code:
// Preferences as singleton
pref = Prefs.getInstance(this.getApplicationContext());
pref.loadSettings();
// Set up the location
loc = new Locations(pref);
loc.setCallback(this);
map = new MyMap(pref);
It seems to work fine. But inside the MyMap class every time I start the app a null pointer exception is thrown. When I am calling MyMap() the following code will be executed:
Code:
[...]
private Prefs pref;
private GoogleMap mMap;
[...]
public MyMap(Prefs prefs) {
pref = (Prefs) prefs;
if (mMap == null) {
FragmentManager fmanager = getSupportFragmentManager();
mMap = ((SupportMapFragment) fmanager.findFragmentById(R.id.map)).getMap();
[...]
}
The line with the findFragmentById is the one that causes the exception.
If I write
Code:
SupportMapFragment f = ((SupportMapFragment) fmanager.findFragmentById(R.id.map));
f is allways null. But how can I access the fragments and view elements defined within my MainActivity?
It works if I put the code inside my MainAcitivity.
Every class extends "android.support.v4.app.FragmentActivity"
I tried to save the application context within my Prefs() class, so that I can access it from everywhere.
But I don't know how to use it inside my additional classes.
How to share the "R" across all my classes?
Can someone help me please?
Thank you very much!!
Thorsten
Are you having trouble adding a Map to a Fragment? If so, then you may take a look at this tutorial. I haven't tried it myself since I couldn't install Google Play Services on my development device. If it helps, do write back, as I am definitely going to try it myself soon.

How to add list view items to an array list? or a work around to achieve the below?

In the below code, where the item’s position and title is getting saved in list_items works fine if list_items and items are ArrayList. For ex;
Code:
ArrayList.add(ArrayList.get(position));
But I am trying to use listview:
Code:
ArrayList.add(Listview.get(position));
The problem exists on line # 10
Code:
1. final Listadapter Adapter = new Listadapter(this,packageList1, packageManager);
2. items.setAdapter(Adapter);
3. items.setChoiceMode(apps.CHOICE_MODE_MULTIPLE_MODAL);

4. items.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
5. @Override
6. public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

7. count = count +1;

8. mode.setTitle(count + "items selected");
9. //problem resides in the below line

10. [B]list_items.add(items.get(position));[/B]

11. }

In the above code, on selecting an item from listview I am trying to save it in an Arraylist i.e list_items which could be used in onActionItemClicked method for further action on that respective item.
Any ideas on how to solve or find a workaround to use a listview in the above situation?

Reflection from one app to another app - Access data

I am trying to figure out how to use reflection to access field data from another apk.
Both apks are in debug and signed with the same certificate and they are sharing the same user id. I built out the method to use reflection to retrieve the field value from the other apk.
Code:
public Class<?> getClass(String remoteClass) throws Exception {
Context remoteContext = mContext.createPackageContext(REMOTE_PACKAGE,
Context.CONTEXT_IGNORE_SECURITY | Context.CONTEXT_INCLUDE_CODE);
ClassLoader loader = remoteContext.getClassLoader();
Class<?> cls = loader.loadClass(remoteClass);
return cls;
}
public Object getField(String obfuscatedClass, String obfuscatedField) throws Exception {
try {
Field field = this.dexLoader.getClass(obfuscatedClass).getDeclaredField(obfuscatedField);
field.setAccessible(true);
return field.get(this.reference);
} catch (NullPointerException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException
| SecurityException | ClassNotFoundException e) {
}
return null;
}
Example:
A - APK (A)
In APK (A) I am running the application and while it is running their is real-time data being set...
B - APK (B)
With APK (B) I have the method getClass in which I will use to access the package from APK (A) and then pull the declared field in the package.
The issue is every time I run the apk that uses the reflection on the running application it just returns the default value that the field started with. In java you can use reflection to access a jvm's field values/methods while it is running. I am trying to do the same approach with the android applications but it doesnt look like it is returning real-time data.
Am I doing this all wrong or is this just not possible?
Another thing I am able to do is decompile the running application and make any additions/modifications I want and then recompile. Is there anything I can do to be able to use reflection to retrieve the data from the field values in real-time. Everything is public and static in the apk.
Thank you for your time to whoever is reading this!
Maybe if I were to inject into the other APK reflection methods and then I can use the other apk to invoke the reflection method with reflection to pull data?

Tips for Creating a Link of App Linking Using the App Linking SDK

What Is App Linking​App Linking allows you to create redirection links that work across multiple platforms including Android, iOS, HarmonyOS, and web. With links of App Linking, you can redirect users to ads, or native app content that they can share with each other. You can create app links and send them to users, or allow users to share links dynamically generated in your app. Anyone who receives a link can tap it to access the specific app content.
Enabling App Linking and Integrating the App Linking SDK​1. Click My projects in AppGallery Connect, click your project card, and select your app for which you want to enable App Linking from the drop-down list on the top.
2. Go to Grow > App Linking. If it is the first time that you use the service, click Use now.
3. Enter the App Store ID and team ID used in your signing certificate, and then click ☑.
4. Go to Project settings > General information, and download the agconnect-services.plist file under App information.
5. Open the CLI and navigate to the location of your Xcode project. Then, create a Podfile. Skip this step if a Podfile already exists.
cd project-directory
pod init
6. Edit the Podfile to add the pod dependency AGConnectAppLinking.
target 'AGC-AppLinking-2' do
pod 'AGConnectAppLinking'
end
7. Install the pod and open the .xcworkspace file to view the project.
pod install
Designing the UI​You can create a page layout in your iOS project and design the UI according to the following figure. On the page, a link of App Linking can be received and displayed.
Requesting a URL Prefix​1. Go to Grow > App Linking. Click the URL prefixes tab and click New URL prefix.
2. In the Set domain name area, enter a URL prefix.
3. Click Next. The system automatically checks whether the URL prefix is available.
Creating a Link of App Linking in Your App​1. Import AGConnectCore and AGConnectAppLinking to the AppDelegate class of the app, and call AGCInstance.startUp in the didFinishLaunchingWithOptions method for initialization.
import AGConnectCore
import AGConnectAppLinking
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
AGCInstance.startUp()
return true
}
2. Create an object named AGCAppLinkingComponents, and set urlPrefix to the URL prefix requested in AppGallery Connect. Set deepLink. Set iOS app parameters, where iosBundleId is the app package name, and iosDeepLink is the redirection link to your iOS app.
let components = AGCAppLinkingComponents()
components.uriPrefix = "https://codelab.drcn.agconnect.link"
components.deepLink = "https://developer.huawei.com/consumer/cn"
components.iosBundleId = Bundle.main.bundleIdentifier
components.iosDeepLink = "AppLinking://ios/test2=456"
components.socialTitle = "AppLinking"
3. Call components.buildLongLink to generate and obtain a long link.
longLinkLabel.text = components.buildLongLink().absoluteString
4. Call components.buildShortLink to generate and obtain a short link.
components.buildShortLink { [self] (shortLink, error) in
if let e = error {
let alert = UIAlertController.init(title: "Error", message: e.localizedDescription, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
return
}
shortAppLinking = shortLink?.url.absoluteString
shortLinkLabel.text = shortAppLinking
}
5. In the method of sharing a link, the short link, generated by the App Linking SDK, is copied to the system clipboard.
@objc func shareLink() {
UIPasteboard.general.string = shortAppLinking
}
Receiving a Link of App Linking in Your App​1. If you want the link of App Linking to be a custom URL, set a custom URL scheme under TARGETS > Info > URL Types in Xcode. Set URL Schemes to AppLinking.
2. Import AGConnectCore and AGConnectAppLinking to the AppDelegate class of the app, and call AGCInstance.startUp in the didFinishLaunchingWithOptions method for initialization.
import AGConnectCore
import AGConnectAppLinking
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
AGCInstance.startUp()
}
3. Call the AGCAppLinking.instance().handle method, and process the received link event in the callback.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let vc = ViewController()
let nav = UINavigationController(rootViewController: vc)
self.window?.rootViewController = nav
AGCInstance.startUp()
AGCAppLinking.instance().handle { (link, error) in
let deepLink = link?.deepLink
vc.getDeepLink(deeplink: deepLink)
}
self.window?.makeKeyAndVisible()
return true
}
4. Implement the application: openURL: options: method, and return the value of AGCAppLinking.instance().openDeepLinkURL(url).
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
let isAppLinking = AGCAppLinking.instance().openDeepLinkURL(url)
return isAppLinking
}
Compiling and Testing Your App​1. In Xcode, run your app on a mobile phone or simulator.
2. Click Create App Linking to generate a long link and a short link using the App Linking SDK.
3. Click Share short App Linking to copy the short link to the clipboard.
4. Paste the short link to a browser and access the link. Click Open to launch your app as prompted.
Congratulations​Well done. You have successfully built an app that integrates App Linking of AppGallery Connect and learned how to:
Create a sharable link using the App Linking SDK.
Launch your app through the configured link and receive the deep link using the App Linking SDK.

Categories

Resources