如何从Android中的活动启动应用程序?

k

我正在按照本教程http://www.androidhive.info/2014/06/android-facebook-like-custom-listview-feed-using-volley/创建类似于Facebook的新闻提要。在本教程中,Feed会在启动应用程序时自行启动。但是我需要通过单击应用程序中的按钮来启动提要。我只需要做的就是,有了这个AppController类来扩展Application。我想通过LoginActivity类启动它。AppController和LoginActivity类都在我的应用程序包中。我不知道如何执行此操作,因为AppController类不是Activity类。我在应用程序中尝试了以下代码来启动提要,但它在程序包中提供了NameNotFoundException。

Intent loginIntent;
PackageManager manager = getPackageManager();
try {
      loginIntent = manager.getLaunchIntentForPackage("example.com.storyteller.app.AppController");

      if (loginIntent == null)
         throw new PackageManager.NameNotFoundException();
      loginIntent.addCategory(Intent.CATEGORY_LAUNCHER);
      startActivity(loginIntent);
} catch (PackageManager.NameNotFoundException e) {
      Log.e("TAG","feed name error");

}

这是我的清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.com.storyteller">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".MainActivity"/>

这是我的文件层次结构。

等级制度

请谁能在这件事上协助我?任何帮助,将不胜感激。

AppController.java

public class AppController extends Application {

public static final String TAG = AppController.class.getSimpleName();

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
LruBitmapCache mLruBitmapCache;

private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

public ImageLoader getImageLoader() {
    getRequestQueue();
    if (mImageLoader == null) {
        getLruBitmapCache();
        mImageLoader = new ImageLoader(this.mRequestQueue, mLruBitmapCache);
    }

    return this.mImageLoader;
}

public LruBitmapCache getLruBitmapCache() {
    if (mLruBitmapCache == null)
        mLruBitmapCache = new LruBitmapCache();
    return this.mLruBitmapCache;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}

}

MainActivity.java

public class MainActivity extends Activity {

private static final String TAG = MainActivity.class.getSimpleName();
private ListView listView;
private FeedListAdapter listAdapter;
private List<FeedItem> feedItems;
private String URL_FEED = "http://api.androidhive.info/feed/feed.json";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.list);

    feedItems = new ArrayList<FeedItem>();

    listAdapter = new FeedListAdapter(this, feedItems);
    listView.setAdapter(listAdapter);

    // These two lines not needed,
    // just to get the look of facebook (changing background color & hiding the icon)
    //getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3b5998")));
    //getActionBar().setIcon(
    //new ColorDrawable(getResources().getColor(android.R.color.transparent)));

    // We first check for cached request
    Cache cache = AppController.getInstance().getRequestQueue().getCache();
    Cache.Entry entry = cache.get(URL_FEED);
    if (entry != null) {
        // fetch the data from cache
        try {
            String data = new String(entry.data, "UTF-8");
            try {
                parseJsonFeed(new JSONObject(data));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    } else {
        // making fresh volley request and getting json
        JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET,
                URL_FEED, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                VolleyLog.d(TAG, "Response: " + response.toString());
                if (response != null) {
                    parseJsonFeed(response);
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
            }
        });

        // Adding request to volley request queue
        AppController.getInstance().addToRequestQueue(jsonReq);
    }

}

/**
 * Parsing json reponse and passing the data to feed view list adapter
 * */
private void parseJsonFeed(JSONObject response) {
    try {
        JSONArray feedArray = response.getJSONArray("feed");

        for (int i = 0; i < feedArray.length(); i++) {
            JSONObject feedObj = (JSONObject) feedArray.get(i);

            FeedItem item = new FeedItem();
            item.setId(feedObj.getInt("id"));
            item.setName(feedObj.getString("name"));

            // Image might be null sometimes
            String image = feedObj.isNull("image") ? null : feedObj
                    .getString("image");
            item.setImge(image);
            item.setStatus(feedObj.getString("status"));
            item.setProfilePic(feedObj.getString("profilePic"));
            item.setTimeStamp(feedObj.getString("timeStamp"));

            // url might be null sometimes
            String feedUrl = feedObj.isNull("url") ? null : feedObj
                    .getString("url");
            item.setUrl(feedUrl);

            feedItems.add(item);
        }

        // notify data changes to list adapater
        listAdapter.notifyDataSetChanged();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

}

Ajinkya

在清单定义中更改此设置,清单定义首先调用AppController,然后调用category.LAUNCHER在以下位置添加AppController(android:name =“。AppController”)

<application
android:name=".AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
    android:name=".LoginActivity"
    android:label="@string/app_name"
    android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从应用程序启动特定活动

来自分类Dev

Android-从搜索按钮启动活动-从长按选项中删除我的应用程序的活动

来自分类Dev

替换Android中的主要活动后无法启动应用程序

来自分类Dev

PhoneGap应用程序启动时如何启动Android活动?

来自分类Dev

Android:了解应用程序启动时活动如何开始

来自分类Dev

Android应用程序,活动启动滞后于viewpager

来自分类Dev

从Android库模块在主应用程序中启动活动

来自分类Dev

仅在首次启动应用程序时,如何启动具有特定活动的应用程序?

来自分类Dev

用于启动应用程序的活动不存在(无法在Android 5.1.1中打开计算器应用程序)

来自分类Dev

如何从屏幕底部到顶部启动一个Android应用程序(非活动)?

来自分类Dev

设置应用程序如何启动应用程序的未导出活动?

来自分类Dev

如何从Android 30中的另一个应用程序启动活动

来自分类Dev

如何在Android本机应用程序活动中画圆

来自分类Dev

Android应用程序无法启动活动componentinfo

来自分类Dev

如果在Android中关闭了启动活动,如何使应用程序关闭

来自分类Dev

如何跟踪应用程序是否首次在Android中启动?

来自分类Dev

如何从Android应用程序中的主线程以外的其他线程启动新活动?

来自分类Dev

在Android应用程序中关闭活动

来自分类Dev

在scheduleAtFixedRate中启动活动时,应用程序崩溃

来自分类Dev

Android应用程序空指针无法启动活动ComponentInfo

来自分类Dev

在android中-有人关闭应用程序时如何启动活动?

来自分类Dev

从应用程序启动表盘配置活动

来自分类Dev

如何从片段重新启动Android中的应用程序

来自分类Dev

检测应用程序内部启动的活动

来自分类Dev

应用程序启动地图活动后,如何立即在android中的地图中显示我当前的位置

来自分类Dev

将启动器活动隐藏在Android的自定义启动器应用程序中的应用程序列表中

来自分类Dev

Android - 应用程序无法启动活动组件信息

来自分类Dev

如何离开当前应用程序并启动另一个应用程序的活动?

来自分类Dev

如何在启动应用程序时选择一个活动作为启动活动?

Related 相关文章

  1. 1

    从应用程序启动特定活动

  2. 2

    Android-从搜索按钮启动活动-从长按选项中删除我的应用程序的活动

  3. 3

    替换Android中的主要活动后无法启动应用程序

  4. 4

    PhoneGap应用程序启动时如何启动Android活动?

  5. 5

    Android:了解应用程序启动时活动如何开始

  6. 6

    Android应用程序,活动启动滞后于viewpager

  7. 7

    从Android库模块在主应用程序中启动活动

  8. 8

    仅在首次启动应用程序时,如何启动具有特定活动的应用程序?

  9. 9

    用于启动应用程序的活动不存在(无法在Android 5.1.1中打开计算器应用程序)

  10. 10

    如何从屏幕底部到顶部启动一个Android应用程序(非活动)?

  11. 11

    设置应用程序如何启动应用程序的未导出活动?

  12. 12

    如何从Android 30中的另一个应用程序启动活动

  13. 13

    如何在Android本机应用程序活动中画圆

  14. 14

    Android应用程序无法启动活动componentinfo

  15. 15

    如果在Android中关闭了启动活动,如何使应用程序关闭

  16. 16

    如何跟踪应用程序是否首次在Android中启动?

  17. 17

    如何从Android应用程序中的主线程以外的其他线程启动新活动?

  18. 18

    在Android应用程序中关闭活动

  19. 19

    在scheduleAtFixedRate中启动活动时,应用程序崩溃

  20. 20

    Android应用程序空指针无法启动活动ComponentInfo

  21. 21

    在android中-有人关闭应用程序时如何启动活动?

  22. 22

    从应用程序启动表盘配置活动

  23. 23

    如何从片段重新启动Android中的应用程序

  24. 24

    检测应用程序内部启动的活动

  25. 25

    应用程序启动地图活动后,如何立即在android中的地图中显示我当前的位置

  26. 26

    将启动器活动隐藏在Android的自定义启动器应用程序中的应用程序列表中

  27. 27

    Android - 应用程序无法启动活动组件信息

  28. 28

    如何离开当前应用程序并启动另一个应用程序的活动?

  29. 29

    如何在启动应用程序时选择一个活动作为启动活动?

热门标签

归档