0

I am following this tutorial http://www.androidhive.info/2014/06/android-facebook-like-custom-listview-feed-using-volley/ to create a newsfeed like Facebook. In the tutorial, the feed starts by itself when starting the application. But I need to start the feed by clicking a button in my application. Simply what I need to do is, There is this AppController class which extends Application. I want to start it through LoginActivity class. Both AppController and LoginActivity classes are inside my application package. I do not know how to do this because AppController class is not an Activity class. I tried the following code in my application to start the feed but it gives a NameNotFoundException in package.

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");

}

This is my manifest file

<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"/>

This is my file hierarchy.

hierarchy

Please can anyone assist me on this matter? Any help would be appreciated.

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();
    }
}

}

Lak
  • 381
  • 1
  • 5
  • 23
  • http://stackoverflow.com/questions/2209513/how-to-start-activity-in-another-application – Ajinkya Mar 08 '16 at 06:31
  • Package name is correct. AppController class extends Application. Therefore its not an activity. So i guess i cannot use startActivity() method in this case. – Lak Mar 08 '16 at 06:46
  • No that is not i want to do. There is this AppController class which extends Application. I want to start it through LoginActivity class. Both AppController and LoginActivity classes are inside my application package. I do not knw how to do this becuase AppController class is not an Activity class. – Lak Mar 08 '16 at 07:10
  • Can you share AppController & from which Activity class it will call Next Activity – Ajinkya Mar 08 '16 at 07:19
  • MainActivity class needs some data which gets from the AppController class. That is why i need to launch it first. MainActivity class has been added in case if you need to refer it as well – Lak Mar 08 '16 at 07:28

3 Answers3

2

Change this in your manifest definatelty which called first AppController then category.LAUNCHER add the AppController(android:name=".AppController") in

<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>
Ajinkya
  • 1,029
  • 7
  • 15
0

You need replace "example.com.storyteller.app.AppController" with your app package.

The app package was configed at the top of manifest file

Ex :

 <manifest
  xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.google.zxing.client.android"

Or in gradle file

 defaultConfig {
        applicationId "example.com.storyteller.app"
        //applicationId  "com.studio.eyeprotect"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 2
        versionName "1.0.1"
    }

Hope it can help you.

dangbk
  • 1
0

So you need replace code with :

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

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

}
dangbk
  • 1
  • No that is not i want to do. There is this AppController class which extends Application. I want to start it through LoginActivity class. Both AppController and LoginActivity classes are inside my application package. I do not knw how to do this becuase AppController class is not an Activity class. – Lak Mar 08 '16 at 07:09
  • Ah, maybe i understand your idea, please research about : Url Schema – dangbk Mar 08 '16 at 07:24