I am new in Android.
I want to send a sms in pdu mode and i found this code, but some class doesn't Support in android studio (Bold Lines). Can anyone help me ?
private void sendsmsBypdu(String phoneNumber,String message) {
int size;
Field f;
Log.d(TAG,"Retrieving phone instance ...");
ContactsContract.CommonDataKinds.Phone phone = **PhoneFactory.getDefaultPhone();**
/* Get IccSmsInterfaceManager */
Log.d(TAG,"Retrieving SmsInterfaceManager ...");
**IccSmsInterfaceManager ismsm = getIccSmsInterfaceManager();**
try {
Log.d(TAG,"Retrieving mDispatcher ...");
**f = IccSmsInterfaceManager.class.getDeclaredField("mDispatcher");**
f.setAccessible(true);
SmsManager sms_smg = (SmsManager)f.get(ismsm);
Log.d(TAG, "Formatting class 0 SMS ...");
byte[] b = new byte[0];
SmsMessage.SubmitPdu pdus = SmsMessage.getSubmitPdu(null, phoneNumber, message,false);
/* change class to Class 0 */
size = (int)pdus.encodedMessage[2];
size = (size/2) + (size%2);
pdus.encodedMessage[size+5] = (byte)0xF0;
/* send raw pdu */
Log.d(TAG,"Sending SMS via sendRawPdu() ...");
try
{
/* Android 2.2 -> 4.0.* */
Method m = SmsManager.class.getDeclaredMethod("sendRawPdu", b.getClass(), b.getClass(), PendingIntent.class, PendingIntent.class);
m.setAccessible(true);
m.invoke(sms_smg, pdus.encodedScAddress, pdus.encodedMessage, null, null);
}
catch(NoSuchMethodException e)
{
/* Android 4.1.2 */
Method m = SmsManager.class.getDeclaredMethod("sendRawPdu", b.getClass(), b.getClass(), PendingIntent.class, PendingIntent.class, String.class);
m.setAccessible(true);
m.invoke(sms_smg, pdus.encodedScAddress, pdus.encodedMessage, null, null, phoneNumber);
}
Log.d(TAG, "SMS sent");
} catch (SecurityException e) {
Log.e(TAG, "Exception: Security !");
e.printStackTrace();
}
catch (NoSuchFieldException e) {
Log.e(TAG, "Exception: Field mDispatcher not found !");
e.printStackTrace();
} catch (IllegalArgumentException e) {
Log.e(TAG, "Exception: Illegal Argument !");
e.printStackTrace();
} catch (IllegalAccessException e) {
Log.e(TAG, "Exception: Illegal access !");
e.printStackTrace();
} catch (NoSuchMethodException e) {
Log.e(TAG, "Exception: sendRawPdu() not found !");
e.printStackTrace();
} catch (InvocationTargetException e) {
Log.e(TAG, "Exception: cannot invoke sendRawPdu() !");
e.printStackTrace();
}
}
I found my Answer. More About this you can read in Hush SMS
SmsManager sm = SmsManager.getDefault();
// Get method "sendRawPdu"
byte[] bb = new byte[1];
SmsMessage.SubmitPdu mypdu = SmsMessage.getSubmitPdu(null, pNo, msg, true);
size = (int) mypdu.encodedMessage[2];
size = (size / 2) + (size % 2);
mypdu.encodedMessage[size + 5] = (byte) 0xF0;
Log.d(TAG, dumpHexString(mypdu.encodedMessage, 0, mypdu.encodedMessage.toString().length()));
Method m2 = SmsManager.class.getDeclaredMethod("sendRawPdu", bb.getClass(),bb.getClass(),PendingIntent.class,PendingIntent.class,boolean.class,boolean.class);
Log.d("success", "success getting sendRawPdu");
m2.setAccessible(true);
int length = msg.length();
count = length / 160;
int m = length % 160;
if (m != 0) {
count++;
}
m2.invoke(sm, mypdu.encodedScAddress,mypdu.encodedMessage,sentPI,deliveredPI, Boolean.valueOf(true),Boolean.valueOf(true));
Log.d("success", "success sending message");
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Related
I can't find anything to read about /dev/graphics/fb0, below is my code .I already exec "chmod 777 /dev/graphics/fb0" before reading fb0; My test environment is Android 4.0 ,4.3 and 4.4.
Below is my code:
public static synchronized Bitmap getScreenShotBitmap() {
FileInputStream buf = null;
try {
fbFile = new File(FB0FILE1);
buf = new FileInputStream(fbFile);
dStream=new DataInputStream(buf);
dStream.readFully(piex); //Java NULLPOINTException
dStream.close();
for(int i=0;i<piex.length;i+=2)
{
colors[i/2]= (int)0xff000000 +
(int) (((piex[i+1]) << (16))&0x00f80000)+
(int) (((piex[i+1]) << 13)&0x0000e000)+
(int) (((piex[i]) << 5)&0x00001A00)+
(int) (((piex[i]) << 3)&0x000000f8);
}
return Bitmap.createBitmap(colors, screenWidth,screenHeight,Bitmap.Config.RGB_565);
} catch (FileNotFoundException e) {
LogUtil.e(TAG, "FileNotFoundException error",e);
} catch (IOException e) {
LogUtil.e(TAG, "IOException error",e);
}catch (Exception e) {
LogUtil.e(TAG, "Exception error",e);
}
finally {
if(buf!=null){
try {
buf.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return null;
}
Now this is the exception:
I am a newbie.
I'm developing an android (4.2) app to record some audio.
The code seems to run fine the first time, and it even saves the files on the SD Card.
But, if I try to record again, my app crashes.
This is my code:
StringVoiceFile = PathImagePage + "/"+book_id+"-"+pagenumber+".mp3";
myAudioRecorder = new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(StringVoiceFile);
this is my function
public void start(){
String PathImagePage = AppDirectory+"/content/"+userid+"/books/"+book_id+"/";
StringVoiceFile = PathImagePage + "/"+book_id+"-"+pagenumber+".mp3";
File VoiceFile = new File(PathImagePage, +"-"+pagenumber+".mp3");
MediaPlayer m = new MediaPlayer();
if (VoiceFile.exists()){
try {
myAudioRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
m.setDataSource(StringVoiceFile);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
myAudioRecorder.start();
} catch (IllegalStateException e) {
Log.e(TAG, "start() failed = " + e );
}
stop.setEnabled(true);
stop.setVisibility(View.VISIBLE);
time.setEnabled(true);
time.setVisibility(View.VISIBLE);
time.setText(formatTime(m.getDuration()));
play.setEnabled(false);
play.setVisibility(View.INVISIBLE);
long now = System.currentTimeMillis();
startRecordingTimer(now);
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_start_again));
} else {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
} catch (IllegalStateException e) {
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_nostart));
} catch (IOException e) {
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_nostart));
}
stop.setEnabled(true);
stop.setVisibility(View.VISIBLE);
time.setVisibility(View.VISIBLE);
time.setText(formatTime(m.getDuration()));
play.setEnabled(false);
play.setVisibility(View.INVISIBLE);
long now = System.currentTimeMillis();
startRecordingTimer(now);
Alert.ToastAlert(FlipActivity.this, getResources().getString(R.string.record_start));
}
}
Please help me to solve this issue
I am extracting metadata information (Exif infor) by using Id3 library in android, but I am getting null pointer exception and getting null in log Message it is a reason because value in src_set is null .I am not getting why it is returning null. my code is :
File src = new File(pathdata);
MusicMetadataSet src_set = null;
try {
src_set = new MyID3().read(src);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} // read metadata
if (src_set == null) // perhaps no metadata
{
Log.i("NULL", "NULL");
}
else
{
try{
IMusicMetadata metadata = src_set.getSimplified();
String artist = metadata.getArtist();
String album = metadata.getAlbum();
String song_title = metadata.getSongTitle();
Number track_number = metadata.getTrackNumber();
Log.i("artist", artist);
Log.i("album", album);
}catch (Exception e) {
e.printStackTrace();
}
File dst = new File(pathdata);
MusicMetadata meta = new MusicMetadata("name");
meta.setAlbum("Chirag");
meta.setArtist("CS");
try {
new MyID3().write(src, dst, src_set, meta);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ID3WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // write updated metadata
}
I am trying to send a file using Android bluetooth sockets. I am getting the pairing requests but unfortunately the data is not being transferred
my Server side and client side is as follows:
Server: (for my application server is receiving the data)
public void run() {
super.run();
BluetoothAdapter mBtadapter = BluetoothAdapter.getDefaultAdapter();
Log.i("bluetooth Adapter", "got adapter "+ mBtadapter.getAddress());
try {
Ssocket = mBtadapter.listenUsingRfcommWithServiceRecord("CardXchange", uuid);
Log.i("Socket", "Socket Acquired "+ Ssocket.toString());
} catch (IOException e) {
e.printStackTrace();
}
while(true){
if(read_flag()==1){
try {
Toast.makeText(c, "flag is eventually 1", Toast.LENGTH_LONG).show();
Ssocket.close();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
try {
client_socket = Ssocket.accept();
if(client_socket!=null){
//this function is used to handle the connection when sockets get connected
manageConnection1(client_socket);
Ssocket.close();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
Log.i("breaked ", "oh god!! out of the loop ");
}
//implementation of the function
private void manageConnection1(BluetoothSocket client_socket2) {
// TODO Auto-generated method stub
int bytes;
Log.i("serevr chya manage connection madhe gela", "in servers manage connection ");
File file = new File("/sdcard/myfolder/received.Xcard");
try {
FileOutputStream fos = new FileOutputStream(file);
try {
InputStream is = client_socket2.getInputStream();
while((bytes = is.read(buffer, 0, 1024))>0){
fos.write(buffer, 0, 1024);
}
is.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
client_socket2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and the client side (the sending side is like this):
public void run(){
try {
Method m = send_dev.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
Csocket = (BluetoothSocket) m.invoke(send_dev, 1);
Csocket.connect();
manageSendConnection(Csocket);
}catch(Exception e){
e.printStackTrace();
}
}
//function to handle after connection is established.
private void manageSendConnection(BluetoothSocket csocket) {
// TODO Auto-generated method stub
File f = new File("/sdcard/myfolder/card3.Xcard");
byte[] buffer = new byte[(int)f.length()];
try {
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
try {
bis.read(buffer, 0, (int)f.length());
OutputStream os = csocket.getOutputStream();
os.write(buffer);
os.close();
fis.close();
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
csocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I am getting the pairing request but the data is not being sent.
can anyone please help me to find the problem and give a hint on solution??
How to read the particular data from the internal storage file.
For eg., I have stored
1. Device 2. Time(epoch format) 3. button text
CharSequence cs =((Button) v).getText();
t = System.currentTimeMillis()/1000;
s = cs.toString();
buf = (t+"\n").getBytes();
buf1 = (s+"\n").getBytes();
try {
FileOutputStream fos = openFileOutput(Filename, Context.MODE_APPEND);
fos.write("DVD".getBytes());
fos.write(tab.getBytes());
fos.write(buf);
fos.write(tab.getBytes());
fos.write(buf1);
//fos.write(tab.getBytes());
//fos.write((R.id.bSix+"\n").getBytes());
fos.write(newline.getBytes());
//fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
then while reading, how can we read only price from the file? (using fos.read())
Thanks
I would suggest writing the file in a more structured way, like this:
long t = System.currentTimeMillis()/1000;
String s = ((Button) v).getText();
DataOutputStream dos = null;
try {
dos = new DataOutputStream(openFileOutput(Filename, Context.MODE_APPEND));
dos.writeUTF("DVD");
dos.writeLong(t); // Write time
dos.writeUTF(s); // Write button text
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
// Ignore
}
}
}
To read it back, something like this:
DataInputStream dis = null;
try {
dis = new DataInputStream(openFileInput(Filename));
String dvd = dis.readUTF();
long time = dis.readLong();
String buttonText = dis.readUTF();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
// Ignore
}
}
}