When to use which constructor for ComponentName in Android?

后端 未结 3 961
夕颜
夕颜 2021-02-01 03:08

I am a little bit confused about the ComponentName class in Android.

There are different ways to get to a component name object, but I don\'t know when to use which... a

3条回答
  •  时光取名叫无心
    2021-02-01 03:43

    Robert Tupelo-Schneck's answer is right about preferring objects against Strings. Here's how I see it.

    • To refer to your own components, use:

      new ComponentName(getApplicationContext(), WidgetProvider.class);
      
    • To refer to some dynamically referenced component in your own app, use:

      // values/strings.xml: de.zordid.sampleapp.widget.WidgetProvider
      String fqcn = getResources().getString(R.string.provider);
      new ComponentName(getApplicationContext(), fqcn);
      

      This is useful when you want to use Android's resource qualifiers to decide which component to use, you can override the default string in values-*/strings.xml.

    • To refer to another application's component, use:

      int componentFlags = GET_ACTIVITIES | GET_PROVIDERS | GET_RECEIVERS | GET_SERVICES;
      PackageInfo otherApp = context.getPackageManager().getPackageInfo("com.other.app", componentFlags);
      ComponentInfo info = otherApp.activities[i]; // or providers/receivers/...
      new ComponentName(info.packageName, info.name);
      

    About .Names and
    • net.twisterrob.app.android.App
    • net.twisterrob.app.android.GlideSetup
    • net.twisterrob.app.android.subpackage.SearchResultsActivity
    • net.twisterrob.app.android.subpackage.Activity
    • net.twisterrob.app.android.content.AppProvider

    on the server side backend of the app and/or some shared model classes:

    • net.twisterrob.app.data.*
    • net.twisterrob.app.backend.*
    • net.twisterrob.app.web.*

    in my Android helper library:

    • net.twisterrob.android.activity.AboutActivity

    other libraries:

    • android.support.v4.content.FileProvider

    This way everything is namespaced in net.twisterrob.app. The android app being just a single part of the whole inside it's own subpackage.

    AndroidManifest.xml (irrelevant parts omitted)

    
    
        
    
        
        
            
            
            
            
            
            
            
            
            
            
            
        
        
    
    

    build.gradle

    android {
        defaultConfig {
            // this is what will be used when you upload it to the Play Store
            applicationId 'net.twisterrob.app'
        }
        buildTypes {
            debug {
                // The neatest trick ever!
                // Released application: net.twisterrob.app
                // IDE built debug application: net.twisterrob.app.debug
                // This will allow you to have your installed released version
                // and sideloaded debug application at the same time working independently.
                // All the ContentProvider authorities within a system must have a unique name 
                // so using ${applicationId} as authority will result in having two different content providers.
                applicationIdSuffix '.debug'
            }
        }
    }
    

    To check out what your final manifest will look like after all the merging open build\intermediates\manifests\full\debug\AndroidManifest.xml.

提交回复
热议问题