Friday, August 24, 2018

How do I get extra data from intent on Android?

How can I send data from one activity (intent) to another?

I use this code to send data:

Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);

Solved

First, get the intent which has started your activity using the getIntent() method:

Intent intent = getIntent();

If your extra data is represented as strings, then you can use intent.getStringExtra(String name) method. In your case:

String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");

In the receiving activity

Bundle extras = getIntent().getExtras(); 
String userName;

if (extras != null) {
    userName = extras.getString("name");
    // and get whatever type user account id is
}

//  How to send value using intent from one class to another class
//  class A(which will send data)
    Intent theIntent = new Intent(this, B.class);
    theIntent.putExtra("name", john);
    startActivity(theIntent);
//  How to get these values in another class
//  Class B
    Intent i= getIntent();
    i.getStringExtra("name");
//  if you log here i than you will get the value of i i.e. john

Add-up

Set Data

String value = "Hello World!";
Intent intent = new Intent(getApplicationContext(), NewActivity.class);
intent.putExtra("sample_name", value);
startActivity(intent);

Get Data

String value;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    value = bundle.getString("sample_name");
}

Instead of initializing another new Intent to receive the data, just do this:

String id = getIntent().getStringExtra("id");

If used in a FragmentActivity, try this:

The first page extends FragmentActivity

Intent Tabdetail = new Intent(getApplicationContext(), ReceivePage.class);
Tabdetail.putExtra("Marker", marker.getTitle().toString());
startActivity(Tabdetail);

In the fragment, you just need to call getActivity() first,

The second page extends Fragment:

String receive = getActivity().getIntent().getExtras().getString("name");

If you are trying to get extra data in fragments then you can try using :

Place data using:

Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER);

Get data using:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


  getArguments().getInt(ARG_SECTION_NUMBER);
  getArguments().getString(ARG_SECTION_STRING);
  getArguments().getBoolean(ARG_SECTION_BOOL);
  getArguments().getChar(ARG_SECTION_CHAR);
  getArguments().getByte(ARG_SECTION_DATA);

}

Just a suggestion:

Instead of using "id" or "name" in your i.putExtra("id".....), I would suggest, when it makes sense, using the current standard fields that can be used with putExtra(), i.e. Intent.EXTRA_something.

A full list can be found at Intent (Android Developers).


We can do it by simple means:

In FirstActivity:

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("uid", uid.toString());
intent.putExtra("pwd", pwd.toString());
startActivity(intent);

In SecondActivity:

    try {
        Intent intent = getIntent();

        String uid = intent.getStringExtra("uid");
        String pwd = intent.getStringExtra("pwd");

    } catch (Exception e) {
        e.printStackTrace();
        Log.e("getStringExtra_EX", e + "");
    }

You can also do like this

// put value in intent

    Intent in = new Intent(MainActivity.this, Booked.class);
    in.putExtra("filter", "Booked");
    startActivity(in);

// get value from intent

    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    String filter = bundle.getString("filter");

Getting Different Types of Extra from Intent

To access data from Intent you should know two things.

  • KEY
  • DataType of your data.

There are different methods in Intent class to extract different kind of data types. It looks like this

getIntent().XXXX(KEY) or intent.XXX(KEY);



So if you know the datatype of your varibale which you set in otherActivity you can use the respective method.

Example to retrieve String in your Activity from Intent

String profileName = getIntent().getStringExtra("SomeKey");

List of different variants of methods for different dataType

You can see the list of available methods in Official Documentation of Intent.


This is for adapter , for activity you just need to change mContext to your Activty name and for fragment you need to change mContext to getActivity()

 public static ArrayList tags_array ;// static array list if you want to pass array data

      public void sendDataBundle(){
            tags_array = new ArrayList();
            tags_array.add("hashtag");//few array data
            tags_array.add("selling");
            tags_array.add("cityname");
            tags_array.add("more");
            tags_array.add("mobile");
            tags_array.add("android");
            tags_array.add("dress");
            Intent su = new Intent(mContext, ViewItemActivity.class);
            Bundle bun1 = new Bundle();
            bun1.putString("product_title","My Product Titile");
            bun1.putString("product_description", "My Product Discription");
            bun1.putString("category", "Product Category");
            bun1.putStringArrayList("hashtag", tags_array);//to pass array list 
            su.putExtras(bun1);
            mContext.startActivity(su);
        }

You can get any type of extra datan from intent, no matter if it's an object or sting or anytype data.

Bundle extra = getIntent().getExtras();
if (extra != null){
    String str1 = (String) extra.get("obj"); // get a object

    String str2 =  extra.getString("string"); //get a string
}

I just posted an answer here that covers this topic in a bit more detail, including some alternatives.

It utilises Vapor API, a new jQuery inspired Android framework I wrote to simplify Android dev. Check out the example in that answer for how you can easily pass data between activites.

Also it demonstrates VaporIntent, which lets you chain method calls and utilise the overloaded .put(...) method:

$.Intent().put("data", "myData").put("more", 568)...

You can easily pass data around your whole application using Vapor API, so hopefully it'll be helpful to you and others during app development.


Monday, August 20, 2018

Block comments in Pug (Jade)?

How do you block comment out code in the Pug templating engine?

I know how to comment out a line:

//-doesn't show

but I don't want to have to write in full html comments like so:


Solved

//-
    Just indent the comment
    like normal elements

Just indent your comment like you do other elements.

Just be careful that you have your indention correct or you may comment out more than you intended.


Sunday, August 19, 2018

JOIN second table to working example

looking to join another table onto existing working example:

Working SQL:

SELECT u.UserName, u.LastName
     , (e.UserName IS NOT NULL) as user_exists_in_EnrollmentsTbl 
  FROM UsersDataTbl u
  LEFT 
  JOIN EnrollmentsTbl e
    ON u.UserName = e.UserName 
        AND e.ClassName LIKE 'Word%'
 WHERE u.Career = 1 AND Active = 1 ORDER BY u.LastName

My Attempt:

(SELECT u.UserName, u.LastName, d.Station
  , (e.UserName IS NOT NULL) as completedl 
  FROM UsersDataTbl u
  LEFT 
  JOIN EnrollmentsTbl e
    ON u.UserName = e.UserName 
        AND e.ClassName LIKE 'Word%') 
  INNER JOIN UsersDataCareerTbl d 
  ON u.UserName = d.UserName
  WHERE u.Career = 1 AND Active = 1 ORDER BY u.LastName

Solved

figured it out:

SELECT u.UserName, u.LastName, d.Station, (e.UserName IS NOT NULL) as completed 
  FROM (UsersDataTbl u
INNER JOIN UsersDataCareerTbl d ON u.UserName = d.UserName)  
LEFT 
  JOIN EnrollmentsTbl e
    ON u.UserName = e.UserName 
        AND e.ClassName LIKE 'Word%' 
 WHERE u.Career = 1 AND Active = 1 ORDER BY u.LastName

Saturday, August 18, 2018

OpsView/Nagios SNMP USB device monitoring

Is there any way to monitor USB devices, connected to the server in Opsview Core? Or there is any software that I can install to make SNMP possible for USB devices?

Some of the USB security dongles often disconnects by themselves. The point is to have notifications when they lost.

Thanks!

Solved

If your USB dongles are connected to the Opsview Server, and if this server is running Linux (i don't know much about Opsview except that it can use nagios plugins), it would probably need only a few lines of code to write a plugin that parses /proc/bus/usb/devices or the output of lsusb and checks for the vendor id / product id / product name of the USB dongles.