继续杂

来源:互联网 发布:测风速的软件 编辑:程序博客网 时间:2024/04/16 05:14

自制短信发送,在通知栏中显示

注意两点:

1.获取的口令如果非系统内部提供,需要在清单中注册

2.手机界面的通知栏显示信息,一定要添加图标(setSmallIcon)

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);            }    public void send(View view){    new Thread(){    public void run() {    SystemClock.sleep(2000);//    获取内容提供者    ContentResolver resolver = getContentResolver();    Uri uri = Uri.parse("content://sms");    //sms系统内部的信息口令,如果用其他口令,要在清单中注册        ContentValues values = new ContentValues();    values.put("address",95588);      //key值不能随意改变    values.put("body", "尊敬的xx女士,你好,你有一笔账单收入,金额为400000元,请查收");    values.put("date", System.currentTimeMillis());    resolver.insert(uri, values);    //    运行在主线程里面的通知栏    NotificationManager manager =    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        NotificationCompat.Builder builder = new     NotificationCompat.Builder(MainActivity.this);        builder.setContentTitle("中国农业银行");    builder.setContentText("尊敬的xx女士,你好,你有一笔账单收入,金额为400000元,请查收");    builder.setSmallIcon(R.drawable.ic_launcher);   //要添加图标,不添加通知栏无法识别,信息就不会显示在通知栏        Notification build = builder.build();    build.flags = Notification.FLAG_AUTO_CANCEL;    manager.notify(1,build);    };    }.start();    }    }

上面的程序没有实现跳转功能


Provider:通过指令对数据库进行数据修改

1.建立数据库类继承SQLiteOpenHelper类

public class BankSqlHelper extends SQLiteOpenHelper {private static String NAME = "Bank";private static int VERSION = 2;public BankSqlHelper(Context context) {super(context, NAME , null, VERSION );}@Overridepublic void onCreate(SQLiteDatabase db) {String sql = "create table bank" +"(_id integer primary key autoincrement, name text, account integer);";db.execSQL(sql);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO}}


2.MainActivity类中执行

 BankSqlHelper helper = new BankSqlHelper(MainActivity.this);        helper.getWritableDatabase();


3.建立provider类继承ContentProvider类

public class BankProvider extends ContentProvider{private static final String TAG = "BankProvider";private BankSqlHelper help;static UriMatcher um = new UriMatcher(UriMatcher.NO_MATCH);  //匹配器static{//          authority: 授权                 path: 要查询的路径       code:um.addURI("com.at.bank.provider.BankProvider", "bank", 100);//um.addURI("com.at.bank.provider.BankProvider", "insurance", 200);}@Overridepublic boolean onCreate() {help = new BankSqlHelper(getContext());if (help != null) {return true;}Log.d(TAG, "创建数据库");return false;}@Overridepublic Uri insert(Uri uri, ContentValues values) {int code = um.match(uri);if (code == 100) {SQLiteDatabase database = help.getWritableDatabase();long insert = database.insert("bank", null, values);if (insert != -1) {Log.d(TAG, "插入成功"+uri.getAuthority());}return ContentUris.withAppendedId(uri, insert);}else{throw new IllegalArgumentException("不可访问");}}@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {int code = um.match(uri);if (code == 100) {SQLiteDatabase database = help.getWritableDatabase();Cursor cursor = database.query("bank", projection, selection,selectionArgs, null, null, sortOrder);Log.d(TAG, "游标遍历,查询");return cursor;}else{throw new IllegalArgumentException("不可访问");}}@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {int code = um.match(uri);if (code == 100) {SQLiteDatabase database = help.getWritableDatabase();int update = database.update("bank", values, selection, selectionArgs);Log.d(TAG, "数据更新");return update;}else{throw new IllegalArgumentException("不可访问");}}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {int code = um.match(uri); if (code == 100) {SQLiteDatabase database = help.getWritableDatabase();int delete = database.delete("bank", selection, selectionArgs);Log.d(TAG, "已删除");return delete;}else{throw new IllegalArgumentException("不可访问");}}@Overridepublic String getType(Uri uri) {// TODO return null;}}


4.创建另一个程序

public class MainActivity extends Activity {    private static final String TAG = "MainActivity";@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    } public void query(View view){    ContentResolver resolver = getContentResolver();    Uri uri = Uri.parse("content://com.at.bank.provider.BankProvider/bank");    Cursor cursor = resolver.query(uri, null, null, null, null);    while (cursor.moveToNext()) {String name = cursor.getString(cursor.getColumnIndex("name"));int account = cursor.getInt(cursor.getColumnIndex("account"));Log.d(TAG, "查询"+name+account);}    }public void update(View view){ContentResolver resolver = getContentResolver();Uri uri = Uri.parse("content://com.at.bank.provider.BankProvider/bank");ContentValues values = new ContentValues();values.put("account", 2000000);int update = resolver.update(uri, values, "name=?", new String[]{"喜茶"});Log.d(TAG, "更新"+update);}public void delete(View view){ContentResolver resolver = getContentResolver();Uri uri = Uri.parse("content://com.at.bank.provider.BankProvider/bank");//返回删除的行数int delete = resolver.delete(uri, "name=?", new String[]{"喜茶"});Log.d(TAG, "删除"+delete);}public void insertin(View view){//获取内部成员ContentResolver resolver = getContentResolver();Uri uri = Uri.parse("content://com.at.bank.provider.BankProvider/bank");    //地址;口令ContentValues values = new ContentValues();values.put("name", "喜茶");values.put("account", 10000000);Uri insert = resolver.insert(uri, values);Log.d(TAG, "添加"+insert.getPath());}}

5.XML

<Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@+id/button1"        android:layout_below="@+id/button1"        android:layout_marginTop="34dp"        android:text="查询"         android:onClick="query"/>    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@+id/button2"        android:layout_below="@+id/button2"        android:layout_marginTop="38dp"        android:text="更新"         android:onClick="update"/>    <Button        android:id="@+id/button4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@+id/button3"        android:layout_below="@+id/button3"        android:layout_marginTop="33dp"        android:text="删除"         android:onClick="delete"/>    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignTop="@+id/textView1"        android:layout_marginLeft="82dp"        android:layout_toRightOf="@+id/textView1"        android:text="添加"        android:onClick="insertin" />





0 0
原创粉丝点击